How to create a login screen

14,090

Solution 1

The problem with any of those solutions is what happens if the user forgets his/her password you could uninstall and reinstall but then you would lose any data for that app which was stored in the app folder. To get arround this you should use the AccountManager.

There is an example of AcconutManger in the sync sample although you would need a method for validating your credentials. Be warned this is not a simple example.

If you are happy that information loss is not a problem then you could just save it to SharedPreferences as suggested by other users.

You should also have a look at all the samples provided in the android sdk folder.

Solution 2

You can use something called sharedPreference, but you might have to encrypt the password and store.

Check , using shared preferences,

http://developer.android.com/guide/topics/data/data-storage.html

Solution 3

You can do that with storing and retrieving data with SQLite database at android, I recommend you to read this tutorial: NotePad Tutorial With Database, it's a very good tutorial.

Solution 4

Use SharedPreferences concept to store password and on start application check it.

First save your details on setup.

public void setSharedPreferences()
{
   try {
       SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("UserData", Context.MODE_PRIVATE);
       SharedPreferences.Editor editor=sharedPreferences.edit();

       editor.putString("FirstName", StringUtils.capitalize(stringFirstName).trim());
       editor.putString("LastName", StringUtils.capitalize(stringLastName).trim());
       editor.putString("Email", stringEmail.toLowerCase().trim());
       editor.putString("Password", stringPassword.trim());

       editor.commit();
   }
   catch (Exception e)
   {

   }
}

Check it on Appliction Startup.

public boolean validateLogin(String email,String password)
{
    try {
        SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("UserData",Context.MODE_PRIVATE);
        if(email.toLowerCase().trim().equals(sharedPreferences.getString("Email",null)) && password.trim().equals(sharedPreferences.getString("Password",null)))
        {
            return true;
        }
        else {
            return false;
        }
    }
    catch (Exception e)
    {
        return false;
    }
}
Share:
14,090
Jonathan
Author by

Jonathan

Updated on August 21, 2022

Comments

  • Jonathan
    Jonathan over 1 year

    I am working on a app and want the user to have to input their password in order to get to the apps main screen. I need to know how I can save their password to a xml or db when they setup their password for the first time and how i can validate if its a correct password when they go to login. Any help I can get would be greatly appreciated. Thanks.