Tuesday

LogOut with Remember login credentials in to Login screen in Android

I faced this problem, when developing android application, So now i have share here , How to remember login credentials in to the login screen after log out click. this will happen only checked button in remember in to the login screen.

In Login.java, there should be one of the rememberme checkbok, which must be check after login button clicked.

This is under the oncreate function

rememberMeCheckBox = (CheckBox) findViewById(R.id.rememberme);

rememberMeCheckBox 
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                            checkStatusBoolean = true;
                            rememberMeCheckBox .setButtonDrawable(R.drawable.check);
                        } else {
                            checkStatusBoolean = false;
                            rememberMeCheckBox   .setButtonDrawable(R.drawable.uncheck);
                        }
                    }
                });



//this is checking after log out button was clicked

if(checkStatusBoolean){
            String ttuser=sharedPreferences.getString("Email", "email->");
            String ttpass=sharedPreferences.getString("Password", "password->");
            userEditText.setText(ttuser);
            passEditText.setText(ttpass);
            rememberMeCheckBox .setButtonDrawable(R.drawable.check);
            Log.d("TAG", "Login credentials remember");
            Log.d("TAG", "Login credentials remember"+"--UN-->"+ttuser+"---_pass__"+ttpass);
        }



No you must set the username(email) and password to SharedPreferences, when login button clicked:



Editor editor = sharedPreferences.edit();
                editor.putString("Email", userEditText.getText().toString()
                        .trim());
                editor.putString("Password", passEditText.getText().toString()
                        .trim());

editir.commit();



Now, Second part is, you have to call Logout, into the second Activity, whenever Logout Button was clicked:

first get SharedPreferences value of email and password.

Button logout;

String  log_emai, log_pass;

public class TestActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

log_email = sharedPreferences.getString("Email", "ema");
         log_pass = sharedPreferences.getString("Password", "pass"); 

logout=(Button)findViewById(R.id.log_out);



logout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

showDialogLogOff();
                      }
        });

 protected void showDialogLogOff() {
       Log_Off custom_Dialog_Out = new Log_off(Test.this);
        // custom_Dialog_Out.setTitle("Log Out");
        custom_Dialog_Out.setMessage(getResources().getString(
                R.string.are_you_sure_you_want_to_logout_));
        TextView noSignOut = (TextView) custom_Dialog_Out
                .findViewById(R.id.no_signout);
        noSignOut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                custom_Dialog_Out.cancel();
            }

        });

        TextView yesSignOut = (TextView) custom_Dialog_Out
                .findViewById(R.id.yes_signout);
        yesSignOut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Test.this.finish();
                RememberLogOut();
            }

        });
        custom_Dialog_Out.show();

    }
    private void RememberLogOut() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("Email",log_email);
        editor.putString("Password", log_pass);
        editor.commit();
        startActivity(new Intent(Test.this,
                Login.class));

    }

 }

}


Happy Coding !!

0 comments

Posts a comment

Related Posts  www.prandroid.com...
 
© 2011 Android Programming Tutorails | 2012 Templates
Designed by Blog Thiết Kế
Back to top