0

I am Learning android development. I am making a registration and login app. my registration and login working properly. But I don't know how to save username and password in shared preference. and set a boolean value to check in slash screen to check login. here my code.

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private final AppCompatActivity activity = LoginActivity.this;

    private NestedScrollView nestedScrollView;

    private TextInputLayout textInputLayoutEmail;
    private TextInputLayout textInputLayoutPassword;

    private TextInputEditText textInputEditTextEmail;
    private TextInputEditText textInputEditTextPassword;

    private AppCompatButton appCompatButtonLogin;

    private AppCompatTextView textViewLinkRegister;

    private InputValidation inputValidation;
    private DatabaseHelper databaseHelper;
    AnimationDrawable animationDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initListeners();
        initObjects();
        animationDrawable=(AnimationDrawable)nestedScrollView.getBackground();
        animationDrawable.setEnterFadeDuration(1000);
        animationDrawable.setExitFadeDuration(1000);
        animationDrawable.start();

        //saving data in shared priference
        SharedPreferences sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("isLogin",true);
        editor.putString("user", String.valueOf(textInputEditTextEmail));
        editor.putString("pass", String.valueOf(textInputEditTextPassword));
        editor.commit();
    }

    /**
     * This method is to initialize views
     */
    private void initViews() {

        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

        textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
        textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);

        textInputEditTextEmail = (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
        textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);

        appCompatButtonLogin = (AppCompatButton) findViewById(R.id.appCompatButtonLogin);

        textViewLinkRegister = (AppCompatTextView) findViewById(R.id.textViewLinkRegister);

    }

    /**
     * This method is to initialize listeners
     */
    private void initListeners() {
        appCompatButtonLogin.setOnClickListener(this);
        textViewLinkRegister.setOnClickListener(this);
    }

    /**
     * This method is to initialize objects to be used
     */
    private void initObjects() {
        databaseHelper = new DatabaseHelper(activity);
        inputValidation = new InputValidation(activity);

    }

    /**
     * This implemented method is to listen the click on view
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.appCompatButtonLogin:
                verifyFromSQLite();
                break;
            case R.id.textViewLinkRegister:
                // Navigate to RegisterActivity
                Intent intentRegister = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intentRegister);
                break;
        }
    }

    /**
     * This method is to validate the input text fields and verify login credentials from SQLite
     */
    private void verifyFromSQLite() {
        if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
            return;
        }
        if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
            return;
        }
        if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_email))) {
            return;
        }

        if (databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
                , textInputEditTextPassword.getText().toString().trim())) {
            Intent accountsIntent = new Intent(activity, HomePage.class);
            accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
            emptyInputEditText();
            startActivity(accountsIntent);
            finish();
        } else {
            // Snack Bar to show success message that record is wrong
            Toast.makeText(getApplicationContext(),getString(R.string.error_valid_email_password),Toast.LENGTH_LONG).show();

        }
    }

    /**
     * This method is to empty all input edit text
     */
    private void emptyInputEditText() {
        textInputEditTextEmail.setText(null);
        textInputEditTextPassword.setText(null);
    }
}

This is my slash screen code.

public class MainActivity extends AppCompatActivity {
    private static  int slash_screen_time_out=4000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
            }
        },slash_screen_time_out);
    }
}

Please help me to resolve this problem. Thank you in advance.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Anuj Mourya
  • 21
  • 1
  • 9
  • Please have a look at this [**answer**](https://stackoverflow.com/questions/9233035/best-option-to-store-username-and-password-in-android-app) –  Oct 16 '17 at 07:15
  • What is your problem exactly ? – Cagri Yalcin Oct 16 '17 at 07:16
  • just a piece of advice, you shouldn't be storing the password on the device, you should use some authtoken or cookies for authentication. – Ashish Ranjan Oct 16 '17 at 07:33
  • I want like remember password if user login in once and after uses user closed the app. but when user open the app again then directly go on the Home page. – Anuj Mourya Oct 16 '17 at 07:37
  • Which is it? `I want to save a username and password in shared preferences .` or `I am a using a Database sqlite`? Are you using shared prefs or an SQL database? – Zoe Oct 16 '17 at 07:41
  • I am using SQLite database. But I want to store username and password in shared preference. @Zoe – Anuj Mourya Oct 16 '17 at 08:28

1 Answers1

0

Save data in shared preferences by this way:

SharedPreferences settings = getSharedPreferences("Login", 0);
settings.edit().putString("user", username).apply();
...

And check data on splash screen

String username = settings.getString("user", "");
if (username.isEmpty()){
   //.. data not saved
} else { 
 //.. data saved
}
Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84