-1

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?

  1. 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);
        }
    });
    
  2. 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);
         }
     });
    
     }
    
Onic Team
  • 1,620
  • 5
  • 26
  • 37
  • not clear, could you specify a sénario of execution? – sansa Jul 25 '17 at 15:29
  • main activity has 3 buttons; login, account, exit. Account button provides the user a sign up activity which appends the username and password into a shared preferences. login button enables if a username and password has been added into a shared preferences. if shared preferences is null, login button is disabled. thanks and my sharedpreferences is declared on my activity 2 extends activity 1 – Ivan Ray Maple Buglosa Jul 25 '17 at 15:45

1 Answers1

0

In 1st Activity check either your shared preference exists or not. first time it will be null.

     btnLogin.setEnabled(false);
     btnAccount.setEnabled(false);
     SharedPreferences mPrefs=this.getSharedPreferences(Your_Preference_Name,Context.MODE_PRIVATE);
    if(mPrefs==null)
    {
        btnAccount.setEnabled(true);
    }
    btnAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });