I have this little login program that saves data into shared preferences. The main activity has "login" and "account" buttons. I want to disable the login button when shared preferences are null and enable the "account" button for the user to sign up for an account which is my second activity. How can I enable/disable 2 buttons if shared preferences are null?
Activity 1
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_interface); final Button login = (Button)findViewById(R.id.Login); final Button account = (Button)findViewById(R.id.Account); login.setEnabled(false); login.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { startActivity(new Intent(MainActivity.this, Summation.class)); } }); account.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { startActivity(new Intent(MainActivity.this, Append.class)); } }); Button exit = (Button)findViewById(R.id.Exit); exit.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { finish(); System.exit(0); } });
Activity 2
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Stetho.initializeWithDefaults(this); setContentView(R.layout.append_interface); final EditText usernameAppend = (EditText)findViewById(R.id.usernameAppend); final EditText passwordAppend = (EditText)findViewById(R.id.passwordAppend); //this buttons appends the username and password to the SharedPreferences Button append = (Button)findViewById(R.id.Append); append.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { final SharedPreferences mySharedPref = getSharedPreferences(PREF_NAME, MODE_PRIVATE); final SharedPreferences.Editor editor = mySharedPref.edit(); //Append get data final String un = usernameAppend.getText().toString(); editor.putString(editorUsername,un); editor.commit(); final String pw = passwordAppend.getText().toString(); editor.putString(editorPassword, pw); editor.commit(); if(mySharedPref != null){ Toast.makeText(Append.this,"Account saved",Toast.LENGTH_LONG).show(); startActivity(new Intent(Append.this, MainActivity.class)); }else{ Toast.makeText(Append.this,"Please enter again",Toast.LENGTH_LONG).show(); } } } ); //Exit button,, exits the app Button exit = (Button)findViewById(R.id.Exit); exit.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { finish(); System.exit(0); } }); }