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:
statethe initial state of the button (enum)disabledenabledloadingsuccessfailed
disabledTexttext to display when the button is disabledenabledTexttext to display when the button is enabledloadingTexttext to display when the button is loadingsuccessTexttext to display when the button is in success statefailedTexttext to display when the button is in failed statedisabledTextColorcolor of disabled text (resource id)enabledTextColorcolor of enabled text (resource id)loadingTextColorcolor of loading text (resource id)successTextColorcolor of success text (resource id)failedTextColorcolor of failed text (resource id)disabledBackgroundbackground for disabled stateenabledBackgroundbackground for enabled stateloadingBackgroundbackground for loading statesuccessBackgroundbackground for success statefailedBackgroundbackground for failed statedisabledIconicon for disabled stateenabledIconicon for enabled stateloadingIconicon for loading statesuccessIconicon for success statefailedIconicon for failed statedisabledIconColoricon color for disabled stateenabledIconColoricon color for enabled stateloadingIconColoricon color for loading statesuccessIconColoricon color for success statefailedIconColoricon color for failed statedisabledIconVisibleicon visibility for disabled state (boolean)enabledIconVisibleicon visibility for enabled state (boolean)loadingIconVisibleicon visibility for loading state (boolean)successIconVisibleicon visibility for success state (boolean)failedIconVisibleicon 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_redStyleStateButton_md_blueStyleStateButton_md_pinkStyleStateButton_md_purpleStyleStateButton_md_deeppurpleStyleStateButton_md_indigoStyleStateButton_md_lightblueStyleStateButton_md_cyanStyleStateButton_md_tealStyleStateButton_md_greenStyleStateButton_md_lightgreenStyleStateButton_md_limeStyleStateButton_md_yellowStyleStateButton_md_amberStyleStateButton_md_orangeStyleStateButton_md_deeporangeStyleStateButton_md_brownStyleStateButton_md_greyStyleStateButton_md_bluegreyStyle