Skip to content Skip to sidebar Skip to footer

Disable Buttons Permanently Throughout The Aplication In Android

Actually i have 3 buttons.User should click on any one button then all the 3 buttons should disable permanently throughout the app(when we close and open the app, buttons should be

Solution 1:

define the behavior in SharePreferences:

for example use this in onResume:

SharedPreferencespref= PreferenceManager.getDefaultSharedPreferences(context);
booleanenabled= pref.getBoolean("isEnabled",true);
myButton.setEnabled(enabled);

in onClick event of the button do this:

SharedPreferencespref= PreferenceManager.getDefaultSharedPreferences(context);
pref.edit().putBoolean("isEnabled",false).commit();
myButton.setEnabled(false);

Solution 2:

Use shared preference to store the clicked state of button.And check the preference value each time in activity/ fragment and disable or enable as per preference value.

Solution 3:

you need to save your button state in sharedpreferences and based on your condition you need to enable / disable it in your activity code.

if(stateofbuttonfromprefs) {
    button.setEnabled(false);
} else {
    button.setEnabled(true);
}

Solution 4:

You can use SharedPreference for your purpose. For more information refer this

declare SharedPreference before onCreate method

SharedPreferences stateButton;
SharedPreferences.Editor bEditor;

initialize this on onCreate()

stateButton= getApplicationContext().getSharedPreferences("Button_State", 0);bEditor = stateButton.edit();

add these two methods on your activity

publicvoidsetBState(boolean e) {
    bEditor.putBoolean("btn_state", e);
    bEditor.commit();
}

publicbooleangetButState(){
    return stateButton.getBoolean("btn_state", true);
}

call this to know your button state call

but.setEnabled(getBState());

when you need to disable the button, use

setBState(false);

Solution 5:

On Button click

button_login.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            SharedPreferencesprefs= PreferenceManager.getDefaultSharedPreferences(); 
            prefs.edit().putBoolean("btn_click", true).commit();
        }
    });

In Activity OnCreate method

Boolean btnClick= prefs.getBoolean("btn_click", false);
if(btnClick){
     //Disable Button
}else{
     //Enable Button
}

Post a Comment for "Disable Buttons Permanently Throughout The Aplication In Android"