Skip to content

Repository files navigation

Progress Button █▒▒▒▒▒▒▒

ProgressButton licenceprogress-button-versionProgressButton forksProgressButton starsProgressButton issuesProgressButton pull-requests

Report Bug · Request Feature

Need easy way to refelect all the button states?

Progress Button is an android library for handling different types states like active, finished, enabled, disabled, and reset with a single line of code.

🚀 Demo

🧐 Features

  • One line of code to change state
  • Easy configurable
  • Customizable
  • Set vibration on click
  • Disable views in active state

🛠️ Installation Steps

repositories { maven { url 'https://jitpack.io' } } dependencies { implementation 'com.github.hellosagar:ProgressButton:latest_version'
} 

💻 Usage

In XMl you need to define the button with your parameters to achieve the desired the design

Notes -

  • To use vibration on click please add the following permission in android manifest
 <uses-permissionandroid:name="android.permission.VIBRATE"/>

Here is the sample code

<dev.sagar.progress_button.ProgressButton
android:id="@+id/button"android:layout_width="240dp"android:layout_height="54dp"app:btn_elevation="12dp"app:corner_radius="12dp"app:default_text="YOOYOOYOYOYO"app:disabled_color="@android:color/holo_purple"app:finish_text="Im done"app:is_vibrate="false"app:finished_color="@color/gray_700"app:pressed_color="@color/black_500"app:stroke_color="@android:color/holo_orange_dark"app:stroke_width="3dp" />

Color Parameters

ExplanationParameter NameTypeDefault Value
Set Default Colordefault_colorcolor#0052FE
Set Disabled Colordisabled_colorcolor#537CD3
Set Pressed Colorpressed_colorcolor#0845D1
Set Finished Colorfinished_colorcolor#27AE60
Set Ripple Colorripple_colorcolor@android:color/transparent
Set Text Colorbtn_text_colorcolor#FFFFFF
Set Stroke Colorstroke_colorcolor@android:color/transparent

Text Parameters

ExplanationParameter NameTypeDefault Value
Set Default Textdefault_colortextButton
Set Finished Textfinish_texttextFinish
Set Text Sizebtn_text_sizedimension14sp

Vibration Parameters

ExplanationParameter NameTypeDefault Value
Is Vibration enabledis_vibratebooleanfalse
Set Vibration time in (ms)finish_textinteger30

Misc Button Parameters

ExplanationParameter NameTypeDefault Value
Set Stroke Widthstroke_widthdimension0dp
Set Corner Radiuscorner_radiusdimension10dp
Set Button Elevationbtn_elevationdimension0dp

Here is the code sample to understand on how to change the button state

classMainActivity : AppCompatActivity() {
privatelateinitvar binding:ActivityMainBindingoverridefunonCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
binding =ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initViews()
initClickListeners()
}
privatefuninitViews() = binding.apply {
// Passing list of view that we want them to be disabled during the active state
progressButton.setDisableViews(listOf(
editTextTextPersonName,
editTextTextPersonName2,
))
}
privatefuninitClickListeners() = binding.apply {
progressButton.setOnClickListener {
Toast.makeText(this@MainActivity, "On click!", Toast.LENGTH_SHORT).show()
}
btnLoading.setOnClickListener {
// Loading state
progressButton.loading()
}
btnFinish.setOnClickListener {
// Finish state
progressButton.finished()
}
btnEnable.setOnClickListener {
// Enable state
progressButton.enable()
}
btnDisable.setOnClickListener {
// Enable state
progressButton.disable()
}
btnReset.setOnClickListener {
// Reset state
progressButton.reset()
}
}
}

You can also directly integrate livedata to your button. This will automatically change the state of button according to the livedata.

//This is an enum that contains all button states, use this for livedataenumclassButtonStates {
LOADING, ENABLED, DISABLED, FINISHED
}
//liveData variableval buttonState =MutableLiveData<ButtonStates>()
//In Fragment or Activity
binding.progressButton.attachToLiveData(buttonState)

Just post the button state to the livedata like livedata.postValue(ButtonStates.LOADING) and button will change to loading state automatically

Jetpack Compose

If you are using Jetpack Compose, all you have to do it to call the ProgressButton composable

Composable function

@Composable
funProgressButton(
buttonState:ButtonState,
text:String,
completedText:String,
modifier:Modifier = Modifier,
shape:Shape = RoundedCornerShape(4.dp),
progressBarStrokeWidth:Dp = 3.dp,
progressButtonColors:ProgressButtonColors = ProgressButtonColors(),
progressButtonElevation:ProgressButtonElevation = ProgressButtonElevation(),
textStyle:TextStyle = defaultTextStyle,
onClick: () ->Unit
)

Parameters

ExplanationParamter nameTypeDefault value
Current state of buttonbuttonStateButtonStatenone
Text shown in buttontextStringnone
Text shown in completed StatetextStringnone
Modifier for buttonmodifierModifierModifier
Shape of buttonshapeShapeRoundedCornerShape with 4dp radius
Stroke width of the progress bar inside buttonprogressBarStrokeWidthDp3.dp
Colors used in buttonprogressButtonColorsProgressButtonColorsProgressButtonColors()
Elevation used in button statesprogressButtonElevationProgressButtonElevationProgressButtonElevation()
Text Style of text inside buttontextStyleTextStyleTextStyle(color = Color.White, fontSize = 16.sp, fontWeight = FontWeight.Bold)
Lambda function to be called on button pressonClick() -> Unitnone

Helper Classes

//This class holds data of the colors used in buttondata classProgressButtonColors(
valbackgroundColor:Color = DEFAULT_COLOR, //#0052FEvaldisabledColor:Color = DISABLED_COLOR, //#537CD3valfinishedColor:Color = FINISHED_COLOR, //#27AE60valcontentColor:Color = CONTENT_COLOR//#FFFFFF
)
//This class holds data for the elevation used in button data classProgressButtonElevation(
valdefaultElevation:Dp = DEFAULT_ELEVATION, //8dpvalelevationOnPress:Dp = DEFAULT_ELEVATION_ON_PRESS//12dp
)

Sample code

funMainScreenContent() {
//mutable State to holf button statevar buttonState by remember {
mutableStateOf(ButtonState.DISABLED)
}
ProgressButton(
buttonState = buttonState,
text ="Button",
completedText ="Finished",
shape =RoundedCornerShape(12.dp),
modifier =Modifier.fillMaxWidth()
) {
}
}

🌟 You are all set!

🍰 Contribute

Feel free to fork this project, to optimise the code or to add new features.

📝 TODO

  • Lottie support

🛡️ License

This project is licensed under the MIT License - see the LICENSE file for details.

🙌 Support

This project needs a 🌟 from you

Developed with ❤️ in India 🇮🇳

About

No need to write boilerplate code for handling the different states of button 😌

Topics

Resources

Stars

51 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages