-1

I would like to know how I can store a boolean with SharedPreferences and disable a button. If you restart the app, the button should remain disabled. Here is my Code, but something is wrong.

public class Pass extends AppCompatActivity implements View.OnClickListener {

private Button btn1;
private EditText text1;

private SharedPreferences speicher;
private SharedPreferences.Editor editor;

final boolean enabled = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pass);

    btn1 = (Button) findViewById(R.id.button);
    btn1.setOnClickListener(this);

    text1 = (EditText) findViewById(R.id.editText);
    text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

    speicher = getApplicationContext().getSharedPreferences("Daten", 0);
    editor = speicher.edit();

     speicher = getSharedPreferences("Daten", 0);
     speicher.getBoolean("Data1", enabled);

}


public void onClick (View view){


    if (text1.getText().toString().equals("pass")){
        AlertDialog ad = new AlertDialog.Builder(this).create();
        ad.setMessage("Great");
        ad.show();
        Intent intent = new Intent(this,Popup.class);
        startActivity(intent);

        btn1.setEnabled(enabled);

        editor.putBoolean("Data1", enabled);
        editor.commit();

    }else{
        String message = "Wrong";
        Toast.makeText(this,message, Toast.LENGTH_LONG).show();
    }
}

}

I hope you can help me.

since

Strecki

Kavach Chandra
  • 770
  • 1
  • 10
  • 25
Strecki
  • 19
  • 1
  • 6
  • 1
    Possible duplicate of [How to store a boolean value using SharedPreferences in Android?](https://stackoverflow.com/questions/23919338/how-to-store-a-boolean-value-using-sharedpreferences-in-android) –  Aug 08 '17 at 19:23
  • you are already saving boolean. Do u want to know how to retrieve it? – akshay_shahane Aug 08 '17 at 19:23
  • Possible duplicate of https://stackoverflow.com/questions/4967418/using-shared-preferences-editor – Stuckzilla Aug 08 '17 at 19:25
  • You need to do this in your onResume() method: ((Button) findViewById(R.id.button)).setEnabled(speicher.getBoolean("Data1", enabled)); – ABS Aug 08 '17 at 19:29
  • Hi, I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. – Kavach Chandra Aug 08 '17 at 19:44

2 Answers2

1

You're not checking the condition to enable or disable button that you're getting from SharedPreference. Do something like this:

btn1.setEnabled(speicher.getBoolean("Data1", enabled));
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Kavach Chandra
  • 770
  • 1
  • 10
  • 25
0

Try this one it will work.

Remove this line on your code. Because initialized again here.

"speicher = getSharedPreferences("Daten", 0);"

You have to try like this

SharedPreferences speicher = getApplicationContext().getSharedPreferences("Daten", 0);
SharedPreferences.Editor editor = speicher.edit(); 

Store the value like this

editor.putBoolean("Data1", true); editor.commit();

Get value like this

Boolean result = speicher.getBoolean("Data1", false); //false is default value.
Raja
  • 2,775
  • 2
  • 19
  • 31