71 Responses to Android Login Registration Screen with SQLite Database Example

  1. Sameer says:

    Very useful post. I am looking for this simple post long time ago

  2. Meghali says:

    how to check whether the data has been stored in database or not?

    • SmrutiRanjan says:

      Dear Meghali,
      There are varieties of ways to check the stored data. Lets have discuss on Login Example.
      If you are running the app on device/emulator, then connect your device/emulator to your PC.
      Then start Eclipse->DDMS. Then select the connected device/emulator from left side pannel.
      Then goto ‘File Explorer’. then goto ‘data/data/com.techblogon.loginexample/database/login.db’. This file will be in your device and ‘com.techblogon.loginexample’ is the package name.
      Then pull the file and check it using SQLite Browser. I have attached the image below the post. pls see above.

      The best way:Also you can check all SQLite helper class function with try/catch block, if your data has been stored, then no exception will occurred.

    • vyshakh says:

      @Meghali: you can check the database contents from DDMS perpective in Eclipse. DDMs–>file explorer->data->name of your project–>databases–

  3. vyshakh says:

    Brilliant tutorial…Is it a good practice or will there be a problem if we call the signup activity from the main thread although we use intents?

    Btw,it would be great if you could post a tutorial on sending the sql data to remote server as JSON or XML data through a Rest webservice

    • SmrutiRanjan says:

      Dear Vyshakh,
      You can also call the signup activity from the main thread. No issue, but if you need to get some result back then pls use startActivityForResult(Intent, int).
      I will update the tutorial on sending the sql data to remote server as JSON or XML data through a Rest webservice very soon.

      br
      techblogon

  4. Meghali says:

    thank you so much :)

  5. ikhlas says:

    its telling R cannot resolve in HomeActivity

    • SmrutiRanjan says:

      Dear ikhlas,
      You can go through my article ‘Some Common Android Development Errors and Solutions’. that might help you.
      Link: http://techblogon.com/common-android-development-errors-solutions/

      However i have build the code using SDK version Android 4.0.3. your version might be different. so change the version.
      Also you can check the proper xml file names if you just copy the code snippet.
      Did you download the code? or copy paste from the code snippet?
      It is better to create a new project with given project and package name, then replace the content.

      br
      techblogon.

      • ikhlas says:

        Its ok now thANK you….
        Now i these error have pop up in….SignUPActivity.java

        editTextUserName=(EditText)findViewById(R.id.editTextUserName);
        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);

        btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
        //////////////
        editTextUserName
        editTextPassword
        editTextUserName cannot be resolved or is not a field
        buttonCreateAccountcannot be resolved or is not a field
        buttonCreateAccount
        Thank you in advance

        • SmrutiRanjan says:

          Dear ikhlas,
          You might forget to add these lines in the java file “SignUPActivity.Java” as in the above code.
          It may help you.

          EditText editTextUserName,editTextPassword,editTextConfirmPassword;
          Button btnCreateAccount;

        • ikhlas says:

          editTextUserName=(EditText)findViewById(R.id.editTextUserName);
          editTextPassword=(EditText)findViewById(R.id.editTextPassword);
          editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);

          Now its i got problem with id…

          Can i also i have tutorial for splash screen please..
          THank you so much

        • IKHLAS says:

          nOWS ITS TELLING ME cANNOT solve ID,Layout,,

          • SmrutiRanjan says:

            Which sdk version is showing for your project?
            pleas send the exact error logs, so that i can say some solution.

          • IKHLAS says:

            API 17 version4.2.2

          • SmrutiRanjan says:

            change the SDK version as your,s. goto project and right click on it, then property. then choose SDK version. it might help you.

            But i suggest, make a new project and copy code to your project file. make sure all the packages are added in the project

          • Jeelan says:

            I got it bro thank you very much.Now it is working fine. all records got inserted.

            can u tell how to create auto login using shared preferences .I mean if we check the check box while login by the next time when we open the app it has to redirect to home page.

          • SmrutiRanjan says:

            Dear Jeelan,
            It is very simple. When user check the check box for auto login, save the checkbox state in a shared pref. When user open the app next time, first read the state of the checkbox in your homepage(login page)’s oncreate() method before showing the GUI, destroy the login page and then start the required activity directly.

            If you are new to android shared pref, then let me know, i will update an article with example for android shared preference

          • Jeelan says:

            Thank u very much for reply I am new to android could you plz update the code of shared preferences and one more thing is how to retrive feilds from database.

          • SmrutiRanjan says:

            Ok I will update sharedpref very soon.
            However for retrive feilds from database, you might like my article with example.Here in the code we retrieve username from the database
            Link: http://techblogon.com/android-login-registration-screen-with-sqlite-database-example/

          • jeelan says:

            Sqlite database was fantabilous.Is it possible to retive the values in one method {getsingleentry()} .

            Just modifiy the loginpage page using sharedpref so that it will redirect main page.

          • SmrutiRanjan says:

            delete the R.java file , clean project and rebuild.

          • SmrutiRanjan says:

            you can check the xml file name. make sure you don’t have any space in the file name.

  6. Tomjay says:

    this is nice post…
    But, can you help me about login in android with pattern code??

    Thanks… ^_^

  7. purnendu says:

    Nice code and i really thankful to you for helping me though you code…..

  8. sagar says:

    very nice …….so simple and easy to understand
    thank you…

  9. sagar says:

    Sir i am going to develop screen lock…..
    can u help ……??
    how to bring my login activity on screen when user press unlock button?
    pleas…
    thank u…

  10. DroidApp says:

    Thanks for the article. I am facing small problem with the code and wondering if you can help. The app runs fine on my phone but it does not create the database file. basically under Data there is nothing. Would you know what might cause it. I set android.debuggable=”true” in android manifest and put a break point at static final String DATABASE_CREATE but it does not even trigger when I create new account. Thanks for your help

  11. nila says:

    Sir how if i will add form change password?

    • SmrutiRanjan says:

      For change password, you can put an option menu or button, start a new activity, then get user input(like current, new, confirm password). search the new password it in database, then update it, the update function has already written in the code.

  12. balkiss says:

    good tuto

  13. Jeelan says:

    This is login page :Launcher Activity

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;

    public class Login extends Activity
    {
    protected static final Bundle edt = null;
    Button btnSignIn1,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;
    CheckBox ch=null;
    SharedPreferences pre;
    public static String filename=”Myfile”;
    //public static final String PREF_NAME=”store”;
    /*public static final String PREFS_NAME = “MyPrefsFile”;
    public static final String PREF_PWD = “MyPrefsFile”;
    private static final String PREF_USERNAME = “userName”;
    private static final String PREF_PASSWORD = “password”;
    String PROPERTY_CONTENT_TYPE = “Content-Type”;*/

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    pre=getSharedPreferences(filename,0);
    /* SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
    final String userName = pref.getString(PREF_USERNAME, null);
    SharedPreferences pref1 = getSharedPreferences(PREF_PWD,MODE_PRIVATE);
    final String password = pref1.getString(PREF_PASSWORD, null); */
    ch=(CheckBox)findViewById(R.id.checkBox1);
    /* if(ch.isChecked()){
    SharedPreferences settings=getSharedPreferences(“PREF_NAME”,0);
    if(settings.getString(“log”,”").toString().equals(“log”));
    Intent intent = new Intent(Login.this, MainMenu.class);
    startActivity(intent);
    }*/
    // create a instance of SQLite Database
    loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();

    // Get The Refference Of Buttons
    btnSignIn1=(Button)findViewById(R.id.buttonSignIn);
    btnSignUp=(Button)findViewById(R.id.buttonSignUP);

    // Set OnClick Listener on SignUp button
    btnSignUp.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    // TODO Auto-generated method stub

    /// Create Intent for SignUpActivity and Start The Activity
    Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
    startActivity(intentSignUP);
    }
    });

    // Methos to handleClick Event of Sign In Button
    //public void signIn(View V)
    // {
    //final Dialog dialog = new Dialog(Login.this);
    //dialog.setContentView(R.layout.login);
    //dialog.setTitle(“Login”);

    // get the Refferences of views
    /* final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
    final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
    */
    Button btnSignIn1=(Button) findViewById(R.id.buttonSignIn);

    // Set On ClickListener
    btnSignIn1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    // get The User name and Password
    final EditText editTextUserName=(EditText) findViewById(R.id.editTextUserNameToLogin);
    final EditText editTextPassword=(EditText) findViewById(R.id.editTextPasswordToLogin);

    String userName=editTextUserName.getText().toString();
    String password=editTextPassword.getText().toString();

    // fetch the Password form database for respective user name
    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

    // check if the Stored password matches with Password entered by user
    /*if(ch.isChecked())
    {
    SharedPreferences.Editor edt=pre.edit();
    edt.putString(“sharedata1″,userName);
    edt.putString(“sharedata2″,password);
    edt.commit();
    }*/
    /*if(ch.isChecked()){
    if(edt.getString(“sharedata1″,”").toString().equals(“userName”) && edt.getString(“sharedata2″,”").toString().equals(“passowrd”));
    {
    Intent intent = new Intent(Login.this, MainMenu.class);
    startActivity(intent);
    }*/
    /*getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
    .edit()
    .putString(PREF_USERNAME, editTextUserName.getText().toString())
    .putString(PREF_PASSWORD, editTextPassword.getText().toString())
    .commit(); */

    if(password.equals(storedPassword))
    {
    Intent in=new Intent(getApplicationContext(),MainMenu.class);
    startActivity(in);
    finish();
    if(ch.isChecked())
    {
    SharedPreferences.Editor edt=pre.edit();
    edt.putString(“sharedata1″,userName);
    edt.putString(“sharedata2″,password);
    edt.commit();
    /*Intent in1=new Intent(getApplicationContext(),MainMenu.class);
    startActivity(in1);
    finish();*/
    }
    /*Toast.makeText(HomeActivity.this, “Congrats: Login Successfull”, Toast.LENGTH_LONG).show();
    dialog.dismiss();*/
    }
    else
    {
    Toast.makeText(Login.this, “User Name or Password does not match”, Toast.LENGTH_LONG).show();
    }
    }
    });
    }
    /*
    dialog.show();
    }*/

    @Override
    protected void onDestroy() {
    super.onDestroy();
    // Close The Database
    loginDataBaseAdapter.close();
    }
    }

    one more thing i was trying to make autologin using shared preferences but it is not redirecting to main menu when activty starts

    Can u please help me.

    • SmrutiRanjan says:

      Read the auto login state from the main page’s onCreate() function before showing your GUI, if it is true, then start your required activity and destroy the current activity.

  14. rajaa.gerges@hotmail.com says:

    hello man, its a great post. but when i click on the button of the create account everything is successfull but it doesnt take me to the main page.how to do it. thx

  15. gaurav kumar says:

    sir i am finding the error in login.xml & sifnup.xml
    thet net beans says error parsing xml
    so i want the solution of this error in netbeans ide
    where i hav to locate this 2 xml file ????

  16. IKHLAS says:

    hi…its finally working
    Can you please tell me how can i go to another page after login succesfully instead of HOMEactivity…please

    and also if i want to create a new registration form how should i do it…
    thx

    • SmrutiRanjan says:

      It is simple. For showing another page, just start a new activity.

    • IKHLAaS goods says:

      I HAVE ALREADY created a new activity,,,
      After login succesfully i want it to the new activity,,,,
      i have everything but it telling Application stop …..
      i have replace in the login page…where toast congrats login succesfully..
      i replase with the new activity still the same…

  17. Gaurav says:

    Sir i am using ur code for ths login app on device but it is not showing database values and even not picking up values stored in database.it is showing the toast message username or password did not match.please suggest

  18. veeresh sabanna says:

    Hi Smruthi Ranjan

    can you please help me in connecting SQLite database, here Iam using your SQLiteStorageOption project, its running in emulater but problem is it is not showing any data and if i try add option throwing some error.

  19. Urvi Parikh says:

    Sir, I m having a error of com.Linprog.LoginDatabaseAdapter.insertEntry(LoginDatabaseAdapter.java:53)
    plz help me to solve it out.

  20. omar says:

    I want to make a project using Android
    Helps users to chat and communicate with each other
    Can you help me so
    Thank you

  21. Jeelan says:

    could u please update the auto login page using check box (shared preferences).

  22. Jeelan says:

    i have gone through shared preferences tutorials.But am unable to make my requirement.

  23. Amardeep says:

    sir how can we read database table in this project

  24. Amardeep says:

    how to hosting an android project please help me
    Thank you

  25. Amardeep says:

    sir how can we connect the android apps with external database

    • nagaraj says:

      Hi man….. I want to create a gps based vehicle tracker software needed…. please send me the code to me

  26. Bat says:

    i got an error for as follow :

    string password = password.getText().toString();

    it said that i have an error at the getText() baecause it is undefined to string. can u help me?

  27. aown raza says:

    Great Tutorial

  28. Nuziit says:

    my emulator shows LoginDataBaseAdapter cannot resilved to a type……….wii u plz help me

  29. gaurav says:

    what if i want to check if username already exists in my MYSQL database??

  30. gaurav says:

    what if i want to check if username already exists in my MYSQL database??And i also want my password to be of atleast 6 characters.

    help me guys…..

    • SmrutiRanjan says:

      if you want to check existing user name, then query the input user name. API function is already written in the code.
      If yo want to check for at least 6 chars, then it is very simple. Just add a check in your code like [if(6>count){} x)] by counting the input characters before save it into the database

      • GG says:

        i want to prevent insertion of entries if username and password are already inserted in database. I tried to query that but no luck..Any help??

  31. Nitin says:

    Thanks for such a wonderful article. I have a question here. Is the database (that we have created) local to the device where the app is installed? On my registration page I am collecting the user info the very first time the app is installed and I need to store it somewhere just to keep a track of the users. There is no login page or I would say no login is required to use the app (just one time registration during installation). If the SQLite db instance that we are creating above is local to the device that i won’t be able to access the list of users.

    I hope you got my question. Please let me know if you need futher info.

  32. SmrutiRanjan says:

    creating Service in Android is very easy, I am getting so many request to update android service example.
    I will update the same in my blog asap.

  33. Thanx a lot. waiting for ur help

Leave a Reply

Your email address will not be published. Required fields are marked *


+ nine = 10

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Close
Please support the site
By clicking any of these buttons you help our site to get better

Twitter

Facebook

Google+