1

Anyone knows how to save a button's state? I want to enable a button which will be enabled permanently based on certain condition, it should remain enabled even the user exits the application. I already did the enabling of the button. Now how can I save it to make it enabled all through out?

I am setting it enable this way. I am using putExtra on the other class to set a button enable.

Button page2 = (Button) findViewById(R.id.button2);

    Intent intent2=getIntent();

    String isEnabled2 = intent2.getStringExtra("isEnabled2");
    if(isEnabled2==null||isEnabled2.equals("disabled")){
            page2.setEnabled(false);
    }
    else{
            page2.setEnabled(true);
    }

    page2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Intent myIntent = new Intent(view.getContext(), p3.class);
            startActivityForResult(myIntent, 0);

        }
    });

EDIT

Button page2 = (Button) findViewById(R.id.button2);

    Intent intent2=getIntent();
    String isEnabled2 = intent2.getStringExtra("isEnabled2");
    if(isEnabled2==null||isEnabled2.equals("disabled")){
            page2.setEnabled(false);
    }
    else

    {

            page2.setEnabled(true);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Enable.this);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("Name",true);    //name is the key so may use a username or whatever you want 
            editor.commit();  
    }
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean btnEnabled = preferences.getBoolean("Name",false); //false because you probably want that as your default value
    if(btnEnabled)
    {
         page2.setEnabled(true);
    }

    page2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Intent myIntent = new Intent(view.getContext(), p3.class);
            startActivityForResult(myIntent, 0);

        }
    });
Jerome
  • 79
  • 1
  • 8

2 Answers2

2

You can save the state in Shared Preferences then check against the value when the user open the app. Another option would be to store it in SQLite DB if you have a lot of other data that needs to be stored for the user

There are tons of examples on using Shared Prefs so instead of writing another example here is a good one on SO

Shared Prefs Example

Example

if(isEnabled2==null||isEnabled2.equals("disabled")){
        page2.setEnabled(false);
}
else{
        page2.setEnabled(true);
        //create an editor and put your value in there
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("Name",true);    //name is the key so may use a username or whatever you want 
        editor.commit();
     }

Then when you want to retrieve this value

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean btnEnabled = preferences.getBoolean("Name",false); //false because you probably want that as your default value
if(btnEnabled)
{
     page2.setEnabled(true);
}

Something like this should work. I haven't checked it but it should get you going

Edit

To have it enabled to begin, in xml:

android:clickable="false"

or programmatically set it in onCreate() before checking shared prefs

page2.setEnabled(false);
Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • so after the line `page2.setEnabled(true)` I will put a code that will save it from sharedpreference? – Jerome Feb 23 '13 at 16:59
  • That's what I would do. But you have several options. If you only want to save if its try then yes. If you want to save either way, then create a flag in that function and set it to true or false in your `if/else` Hope this makes sense – codeMagic Feb 23 '13 at 17:44
  • I don't understand I'm a newbie in android – Jerome Feb 23 '13 at 18:53
  • That's quite understandable but I need to know what exactly you don't understand. Have you tried to implement the code yet? – codeMagic Feb 23 '13 at 18:55
  • I tried the suggested code on this one but it only sets the button enabled but not saving its state http://stackoverflow.com/questions/14813738/setting-a-button-permanently-enabled – Jerome Feb 23 '13 at 19:08
  • I have edited my answer with a short example. Hopefully something like that will get you started – codeMagic Feb 23 '13 at 19:12
  • I tried the code above but when I run the application, the button is already in enabled state. It should be disabled first and when I set it enabled, that's the time It should be set in enabled. and when I exit the app and reopen, it should still be in enabled state – Jerome Feb 23 '13 at 21:42
0

A better way to save view and other types states would be via the onSavedInstanceState(Bundle state) method and restoring it through the onRestoreInstanceState(Bundle state) method. These both will need to be overriden.

the first method is where you save a state using the bundle state:

state.putString("A KEY ID", "KEY Value");

This is one example on how to save a string. You can save many different types such as boolean, bundles, serializable's etc. Just see whats available using 'state.put' and it should show you a list of methods you can use depending on your IDE.

Then you can restore the state from the second method, onRestoreInstanceState doing what you need to do with the information you have saved, such as, setting text on a view, or displaying the users saved name.

If you got any questions let me know

Sambuxc
  • 425
  • 2
  • 11
  • 26