
Hay,geeks in this tutorial you are going to learn how to authenticate user login information using local database(I mean sqlite). I think if your android database concepts are clear then you can easily authenticate user enter data. On more think also this is not a secure way to authenticate user login, because in this tutorial [...]
Hay,geeks in this tutorial you are going to learn how to authenticate user login information using local database(I mean sqlite).
I think if your android database concepts are clear then you can easily authenticate user enter data.
On more think also this is not a secure way to authenticate user login, because in this tutorial i did’t used password encryption. so if user if like us(mobile geek) can able to see and change password.
Download Full source code
Tutorial step
- Create a simple login form
- Get the data enter by user
- Compere that data to your local db and authenticate login
Your LoginActivity
LoginActivity.java
package com.example.loginfromlocal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final EditText txtUserName = (EditText)findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
try{
if(username.length() > 0 && password.length() >0)
{
DBUserAdapter dbUser = new DBUserAdapter(LoginActivity.this);
dbUser.open();
if(dbUser.Login(username, password))
{
Toast.makeText(LoginActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
dbUser.close();
}
}catch(Exception e)
{
Toast.makeText(LoginActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
Now checking user enter data with database
DBUserAdapter.java
package com.example.loginfromlocal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBUserAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME= "username";
public static final String KEY_PASSWORD = "password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "usersdb";
private static final String DATABASE_TABLE = "users";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table users (_id integer primary key autoincrement, "
+ "username text not null, "
+ "password text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBUserAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public void open() throws SQLException
{
db = DBHelper.getWritableDatabase();
}
public void close()
{
DBHelper.close();
}
public long AddUser(String username, String password)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
}
Download full source code
I think its simple to authentication to local database.
If you want to learn Android login authentication with remote (MySql)database click here !








35 Responses
Hello, A.P
Everything went nice.but one problem is that i think u haven’t mentioned that the content of database should be copied to device.until unless copied it it displays only invalid username and password. after that it works fine. thanks alot
Ohh nice
its just verify by 1 & 0
braja pal told “Everything went nice.but one problem is that i think u haven’t mentioned that the content of database should be copied to device.until unless copied it it displays only invalid username and password. after that it works fine.|”
I just done the above login validation.it shows invalid username and password
so please can u tell me how can copy the content of databaseto device.
ohh i think still you both not getting me , php file file just echo 1 for true login , and 0 for false , android device need to get just 1 or 0
and if you want to get usernae echo “$username”; but its less security
Hello Ajay patel,
I didnt get you.should we create php coding for this i just done the above project in eclipse but it shows invalid usrname and password.I am doing project on this android so please help
Yes you have to create php file
k thanks
I am connecting my application into the local database. So i done copy ur coding and implement in my project but it shows only invalid username and password.
It’s a good tutorial, helps newbies like me to get right on the button with Android SQLite database creation. However you realize when you use the exact code here, the login fails, because data(username, password) are not written into the database, the fields are empty in the database. The method “AddUser()” is not called anywhere. To get it work, I changed it slightly to get me up to speed.
You can use the method this way :
1. In DBUserAdapter.java class
//comment out the arguments and add username,password directly
public long AddUser(/*String username*/ /*String password*/)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, “username”);
initialValues.put(KEY_PASSWORD, “password”);
return db.insert(DATABASE_TABLE, null, initialValues);
}
2.In LoginActivity.java class
Call the method :
Just beneath the :
dbUser.open();
Call the method “AddUser”
dbUser.AddUser();
3. Now when you run the application, you can login with “username” and “password”
4.It is still a brilliantly work done by the author
You can add username and password using the arguments in the method, but then you will
have to use another xml file for getting the user to register/add username and password first
into the database, then you can use the added username and password to login. I wanted it simple so I added the username and password directly into the database avoiding the user registration. But I have learnt a lot from this tutorial and the author is a good teacher, simple , plain and easy to understand
Thanks for sharing this…
use your own check.php file hosted on live server
Thanks to give such a wonderful tutorial…I have one problem with this…Me too done this task in the same way but it give error like “01-09 18:14:18.337: E/dalvikvm(873): Unable to open stack trace file ‘/data/anr/traces.txt’: Permission denied” How to solve this please help me….
[...] Android Login Authentication With Local Database(Sqlite) [...]
@ sirni you should add this permision in manifest <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" to allow using Log in your app
Thanks for the article..great work..
I copied the code into my exhisting simple two screen app, but my “btnLogin” does nothing when clicked. Eclipse shows no errors when run. Since I am new to this, this is what I don’t understand: 1) do I have to create a DATABASE with username and password fields? 2) if yes, how exactly do I do that? I created a Db using Mozilla SQL manager, but don’t know how to import it to Eclipse. This is what I have. When the app is run, it opens main.xml with Login button. When you press login, the app takes you to the login screen where I have the username text box, password text box and the “btnLogin” that does nothing when clicked. Any help is appreciated. Thank you.
I created a new project with your code only, and I still can’t seem to get the lgnButton working. When clicked….nothing. You mentioned the PHP file. How do I create that file and where?
can we have a code for registre user with android/php/mysql
Hi Ajay,
I am very new to android. I want to learn it. I have been learning the layouts. Can you please guide me where to start and how should i go? Can you please tell me how create the database in android using SQLite with explanation…. Can you be my mentor please… Please do mail me at purplerain.moon@gmail.com
Thank you,
Khalid
in android how to create php file .i am new to android. help me
Really nice article
Every article on your blog is absolutely readable. I can learn something new here everyday.
Thank you very much!
Hi Ajay,
I’m new to android. can u send me the code of this example on nileshb4borse@gmail.com.
If you are going for best contents like myself, simply
go to see this web site every day for the reason that it offers quality contents, thanks
[...] Android Login Authentication With Local Database(Sqlite) [...]
excellent… Thank You so much..
Iopen your project, and get 1 error that content : “The method onClick(View) of type new View.OnClickListener(){} must override a superclass method”. Fixed remove @Override
Can you explain and help me fixed it. Thanks
I fixed the error , but now when running in my eclipse using ADV android 4.1 and SDK 20.0.3 it says : “Unfortunately loginform has stopped”. Thanks
I have tried your code and it works fine, except one point. I have tried to save a new userName and password (i.e. userName: Sabri, password: maverick) and when I tried to login, it gives me invalid userName or password. Somehow it is only saving the String “userName + password” in the SQLite database and not any other user or password I save to the SQLite database. Any assitance on this would be very helpful.
HI Ajay, great job this above code is working nice…. i don’t no anything about SQLLite so can u please tell me …. where these username and password is stored and how can i see these username and passwords… i am developing a registration page so it wil be very helpfull please help me ….. thanks in advance… and happy dasara
@Sabri:
how u displayed data in Sqllite please tell me…
Hey @Ritesh!
I figured out the problem a few hours later! But, thanks for replying.
No need to create php file
just replace
if(username.equals(username) && password.equals(password))
in your code
It will be helpfull
plz provide full code..
its not responding
showing force close error.
it seems the program that u have coded is usefull only for those who know php ….could u pls help me out without using php