Android button suitable for network operations
Add the following lines to your build.gradle
repositories {
maven {
url 'https://dl.bintray.com/andrea-rosa/maven/'
}
}
dependencies {
compile 'org.altervista.andrearosa:statebutton:1.0.0'
}
Put in your layout.xml
:
<org.altervista.andrearosa.statebutton.StateButton
android:id="@+id/mButton"
app:state="enabled"
...
/>
Then, in your Activity/Fragment:
...
public class MainActivity extends AppCompatActivity {
private StateButton mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (StateButton) findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// check if you can click on the button
if(mButton.getState() == StateButton.BUTTON_STATES.ENABLED) {
// set it to loading state
mButton.setState(StateButton.BUTTON_STATES.LOADING);
// do stuff
if(/*get some error*/) {
// show the failed state
mButton.setState(StateButton.BUTTON_STATES.FAILED);
} else {
// show the success state
mButton.setState(StateButton.BUTTON_STATES.SUCCESS);
}
// than you can set the button to the original state
mButton.setState(StateButton.BUTTON_STATES.ENABLED);
}
}
});
}
...
}
You can define the following attributes:
state
the initial state of the button (enum)disabled
enabled
loading
success
failed
disabledText
text to display when the button is disabledenabledText
text to display when the button is enabledloadingText
text to display when the button is loadingsuccessText
text to display when the button is in success statefailedText
text to display when the button is in failed statedisabledTextColor
color of disabled text (resource id)enabledTextColor
color of enabled text (resource id)loadingTextColor
color of loading text (resource id)successTextColor
color of success text (resource id)failedTextColor
color of failed text (resource id)disabledBackground
background for disabled stateenabledBackground
background for enabled stateloadingBackground
background for loading statesuccessBackground
background for success statefailedBackground
background for failed statedisabledIcon
icon for disabled stateenabledIcon
icon for enabled stateloadingIcon
icon for loading statesuccessIcon
icon for success statefailedIcon
icon for failed statedisabledIconColor
icon color for disabled stateenabledIconColor
icon color for enabled stateloadingIconColor
icon color for loading statesuccessIconColor
icon color for success statefailedIconColor
icon color for failed statedisabledIconVisible
icon visibility for disabled state (boolean)enabledIconVisible
icon visibility for enabled state (boolean)loadingIconVisible
icon visibility for loading state (boolean)successIconVisible
icon visibility for success state (boolean)failedIconVisible
icon visibility for failed state (boolean)
To much settings?
Don't worry! There are 19 predefined styles that you can use.
Just add style
attribute in your xml and the texts for states:
<org.altervista.andrearosa.statebutton.StateButton
style="@style/StateButton_md_redStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:disabledText="Disbled"
app:enabledText="Click me!"
app:failedText="Failed"
app:loadingText="Loading"
app:successText="Success"
app:state="enabled" />
Predefined styles are:
StateButton_md_redStyle
StateButton_md_blueStyle
StateButton_md_pinkStyle
StateButton_md_purpleStyle
StateButton_md_deeppurpleStyle
StateButton_md_indigoStyle
StateButton_md_lightblueStyle
StateButton_md_cyanStyle
StateButton_md_tealStyle
StateButton_md_greenStyle
StateButton_md_lightgreenStyle
StateButton_md_limeStyle
StateButton_md_yellowStyle
StateButton_md_amberStyle
StateButton_md_orangeStyle
StateButton_md_deeporangeStyle
StateButton_md_brownStyle
StateButton_md_greyStyle
StateButton_md_bluegreyStyle