Login and Registration using Sqlite Database with Forget Password

Introduction 

     In my previous article SQLiteManager plugin for eclipse i explained about how to view inserted sqlite database values in eclipse,now i am going to explain how to insert datas into sqlite database and how to check inserted sqlite database values.

demo
                                                                     Main Screen
           
                                                              
                                                                Registration Screen




                                                             Forget Password Screen






Using the code


activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D8D8"
tools:context=".MainActivity" >

<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="60dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />

<EditText
android:id="@+id/password_edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="30dp"
android:background="@drawable/background_round_corner_edittext"
android:ems="10"
android:inputType="textPassword"
android:padding="5dp" >

<requestFocus />
</EditText>
</RelativeLayout>

<RelativeLayout
android:id="@+id/rel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rel1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp" >

<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/Login" />

<Button
android:id="@+id/register_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/register" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rel2"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forgetpassword" />
</RelativeLayout>

</RelativeLayout>

registration.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D8D8"
android:orientation="vertical" >

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />

<EditText
android:id="@+id/password_edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/background_round_corner_edittext"
android:ems="10"
android:inputType="textPassword"
android:maxLength="9"
android:padding="5dp" >
</EditText>

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/showpassword"
android:textColor="#1C1C1C" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/repassword" />

<EditText
android:id="@+id/repassword_edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/background_round_corner_edittext"
android:ems="10"
android:inputType="textPassword"
android:padding="5dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/securityhint" />

<EditText
android:id="@+id/securityhint_edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/background_round_corner_edittext"
android:ems="10"
android:padding="5dp" >
</EditText>
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rel1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" >

<Button
android:id="@+id/register_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/register" />

<Button
android:id="@+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="15dp"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/cancel" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>

</RelativeLayout>

home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D8D8" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:text="Welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout> 

forget_search.xml: 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D8D8"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/entersecurityhint" />

<EditText
android:id="@+id/securityhint_edt"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="25dp"
android:background="@drawable/background_round_corner_edittext"
android:ems="10"
android:padding="5dp" >
</EditText>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/securityhint_edt"
android:layout_marginTop="15dp"
android:text="@string/yourpassword" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="15dp"
android:paddingBottom="10dp" >

<Button
android:id="@+id/getpassword_btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/getpassword" />

<Button
android:id="@+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/background_round_corner_button"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/cancel" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

Java Codes:

MainActivity.java
package com.example.loginregisterwithsqlite;

import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

LoginDataBaseAdapter loginDataBaseAdapter;
Button login;
Button registerr;
EditText enterpassword;
TextView forgetpass;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

login=(Button)findViewById(R.id.login_btn);
registerr=(Button)findViewById(R.id.register_btn);
enterpassword=(EditText)findViewById(R.id.password_edt);
forgetpass=(TextView)findViewById(R.id.textView2);

loginDataBaseAdapter = new LoginDataBaseAdapter(getApplicationContext());
loginDataBaseAdapter.open();

registerr.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,Registration.class);
startActivity(i);
}
});

login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Password=enterpassword.getText().toString();

String storedPassword=loginDataBaseAdapter.getSinlgeEntry(Password);

if(Password.equals(storedPassword))
{
Toast.makeText(MainActivity.this"Congrats: Login Successfully", Toast.LENGTH_LONG).show();
Intent ii=new Intent(MainActivity.this,Home.class);
startActivity(ii);
}
else
if(Password.equals("")){
Toast.makeText(MainActivity.this"Please Enter Your Password", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this"Password Incorrect", Toast.LENGTH_LONG).show();
}
}
});

forgetpass.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

final Dialog dialog = new Dialog(MainActivity.this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.forget_search);
dialog.show();

final EditText security=(EditText)dialog.findViewById(R.id.securityhint_edt);
final TextView getpass=(TextView)dialog.findViewById(R.id.textView3);

Button ok=(Button)dialog.findViewById(R.id.getpassword_btn);
Button cancel=(Button)dialog.findViewById(R.id.cancel_btn);

ok.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String userName=security.getText().toString();
if(userName.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your securityhint", Toast.LENGTH_SHORT).show();
}
else
{
String storedPassword=loginDataBaseAdapter.getAllTags(userName);
if(storedPassword==null)
{
Toast.makeText(getApplicationContext(), "Please enter correct securityhint", Toast.LENGTH_SHORT).show();
}else{
Log.d("GET PASSWORD",storedPassword);
getpass.setText(storedPassword);
}
}
}
});
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

dialog.show();
}
});
}

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

}

Registration.java
package com.example.loginregisterwithsqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Registration extends Activity{

LoginDataBaseAdapter loginDataBaseAdapter;
EditText password,repassword,securityhint;
Button register,cancel,reg_btn;
CheckBox check;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);

loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
password=(EditText)findViewById(R.id.password_edt);
repassword=(EditText)findViewById(R.id.repassword_edt);
securityhint=(EditText)findViewById(R.id.securityhint_edt);
register=(Button)findViewById(R.id.register_btn);
cancel=(Button)findViewById(R.id.cancel_btn);
check=(CheckBox)findViewById(R.id.checkBox1);

check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

if(!isChecked)
{
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});

register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String Pass=password.getText().toString();
String Secu=securityhint.getText().toString();
String Repass=repassword.getText().toString();

if(Pass.equals("")||Repass.equals("")||Secu.equals(""))
{
Toast.makeText(getApplicationContext(), "Fill All Fields", Toast.LENGTH_LONG).show();
return;
}

if(!Pass.equals(Repass))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(Pass, Repass,Secu);

// reg_btn.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Log.d("PASSWORD",Pass);
Log.d("RE PASSWORD",Repass);
Log.d("SECURITY HINT",Secu);
Intent i=new Intent(Registration.this,MainActivity.class);
startActivity(i);
}
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ii=new Intent(Registration.this,MainActivity.class);
startActivity(ii);
}
});
}
}

DataBaseHelper.java
package com.example.loginregisterwithsqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter""Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
onCreate(_db);
}
}

LoginDataBaseAdapter.java
package com.example.loginregisterwithsqlite;

import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter {

static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 3;

static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID integer primary key autoincrement,""PASSWORD text,"+"REPASSWORD text,""SECURITYHINT text) ";

public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;

public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(contextDATABASE_NAMEnullDATABASE_VERSION);

}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String password,String repassword,String securityhint)
{
ContentValues newValues = new ContentValues();
newValues.put("PASSWORD", password);
newValues.put("REPASSWORD",repassword);
newValues.put("SECURITYHINT",securityhint);

db.insert("LOGIN"null, newValues);
}

public int deleteEntry(String password)
{
String where="PASSWORD=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{password}) ;
return numberOFEntriesDeleted;
}

public String getSinlgeEntry(String password)
{
Cursor cursor=db.query("LOGIN"null" PASSWORD=?"new String[]{password}, nullnullnull);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String repassword= cursor.getString(cursor.getColumnIndex("REPASSWORD"));
cursor.close();
return repassword;
}

public String getAllTags(String a) {

Cursor c = db.rawQuery("SELECT * FROM " + "LOGIN" + " where SECURITYHINT = '" +a + "'" , null);
String str = null;
if (c.moveToFirst()) {
do {
str = c.getString(c.getColumnIndex("PASSWORD"));
while (c.moveToNext());
}
return str;
}

public void updateEntry(String password,String repassword)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("PASSWORD", password);
updatedValues.put("REPASSWORD",repassword);
updatedValues.put("SECURITYHINT",repassword);

String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{password});
}

public HashMap<String, String> getAnimalInfo(String id) {
HashMap<String, String> wordList = new HashMap<String, String>();
String selectQuery = "SELECT * FROM LOGIN where SECURITYHINT='"+id+"'";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
wordList.put("PASSWORD", cursor.getString(1));
while (cursor.moveToNext());
}
return wordList;
}
}

Home.java
package com.example.loginregisterwithsqlite;

import android.app.Activity;
import android.os.Bundle;

public class Home extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}

background_round_corner_button.xml

This xml is stored under drawable folder for button background purpose


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="40dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#01A9DB"/>
<corners
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>


background_round_corner_edittext.xml

This xml is stored under drawable folder for edittext background purpose


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#EFFBF2" />

<stroke
android:width="1dp"
android:color="#01A9DB"
/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
/>

</shape>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginregisterwithsqlite"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.loginregisterwithsqlite.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home" >
</activity>
<activity android:name=".Registration" >
</activity>
</application>
</manifest>

                                                              

Previous
Next Post »

40 comments

Write comments
Space SRE
AUTHOR
September 8, 2014 at 7:14 PM delete

hey i found this post very helpful for a newbie like me . But some of the files here are outdated because i implementes java classes they are using element which are no where defined in the layout classes , can you please update the layout classes and res folder because many backgrounds are also missing and it will take so muh of time and effort if i myself tries to fix everything , thanks in advance

Reply
avatar
Kalidoss
AUTHOR
September 9, 2014 at 2:26 PM delete

download updated source code to this link https://github.com/KalidossRJ/Sqlite-Login-and-Registration

Reply
avatar
issacsha
AUTHOR
November 30, 2014 at 11:10 PM delete

Dear kalidoss,

Thanks for your valuable post. when i import your file to my andriod application. I'm getting error like this "F:\AndroidWorkspace\LoginRegisterwithsqlite\res\values-v14\styles.xml:8: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'."

please help me!...
"

Reply
avatar
Kalidoss
AUTHOR
December 1, 2014 at 11:13 AM delete

Hi Issacsha,

Change style theme to, style name="AppTheme" parent="AppBaseTheme" in main values.xml only...if you get error in values-v14 and values-v11 means delete that folders.

Reply
avatar
Anonymous
AUTHOR
December 1, 2014 at 3:43 PM delete

cant you build a source code zip file where there is no errors after importing?

Reply
avatar
Kalidoss
AUTHOR
December 2, 2014 at 11:35 AM delete

Your eclipse and ADT bundle version is different from my eclipse and ADT bundle thats why the style.xml errors occur...

Reply
avatar
Nouha
AUTHOR
February 25, 2015 at 2:03 AM delete

Hi,I would like your code and I would like to add a new field called username and login to the application with username and password

Reply
avatar
Nouha
AUTHOR
February 25, 2015 at 2:03 AM delete

Can you help me please

Reply
avatar
Kalidoss
AUTHOR
February 25, 2015 at 5:41 PM delete

Hi nouha,

Create one more method in login activity to check username and password (same like forget password method).

Reply
avatar
Nouha
AUTHOR
March 3, 2015 at 3:27 AM delete

Hi
I have managed to add a new field (username) and login with username and password
Here are the changes I made ​​in your code

Reply
avatar
Nouha
AUTHOR
March 3, 2015 at 3:28 AM delete

package com.android.dp;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter {

static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 3;

static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID integer primary key autoincrement,"+ "PASSWORD text,"+"REPASSWORD text,"+ "SECURITYHINT text,"+"NAME text) ";

public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;

public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String password,String repassword,String securityhint,String name)
{
ContentValues newValues = new ContentValues();
newValues.put("PASSWORD", password);
newValues.put("REPASSWORD",repassword);
newValues.put("SECURITYHINT",securityhint);
newValues.put("NAME",name);

db.insert("LOGIN", null, newValues);
}

public int deleteEntry(String password)
{
String where="PASSWORD=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{password}) ;
return numberOFEntriesDeleted;
}

public String getSinlgeEntry(String password)
{
Cursor cursor=db.query("LOGIN", null, " PASSWORD=?", new String[]{password}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String repassword= cursor.getString(cursor.getColumnIndex("REPASSWORD"));
cursor.close();
return repassword;
}

public String getAllTags(String a) {

Cursor c = db.rawQuery("SELECT * FROM " + "LOGIN" + " where SECURITYHINT = '" +a + "'" , null);
String str = null;
if (c.moveToFirst()) {
do {
str = c.getString(c.getColumnIndex("PASSWORD"));
} while (c.moveToNext());
}
return str;
}

public void updateEntry(String password,String repassword,String name)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("PASSWORD", password);
updatedValues.put("REPASSWORD",repassword);
updatedValues.put("SECURITYHINT",repassword);
updatedValues.put("NAME",name);




String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{password});
}

public HashMap getAnimalInfo(String id) {
HashMap wordList = new HashMap();
String selectQuery = "SELECT * FROM LOGIN where SECURITYHINT='"+id+"'";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
wordList.put("PASSWORD", cursor.getString(1));
} while (cursor.moveToNext());
}
return wordList;
}
}

Reply
avatar
Nouha
AUTHOR
March 3, 2015 at 3:28 AM delete

package com.android.dp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class Registration extends ActionBarActivity {
LoginDataBaseAdapter loginDataBaseAdapter;
EditText password,repassword,securityhint,na;
Button register,cancel,reg_btn;
CheckBox check;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
setTitle("New Account");
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
password=(EditText)findViewById(R.id.password_edt);
na=(EditText)findViewById(R.id.name_edt);
repassword=(EditText)findViewById(R.id.repassword_edt);
securityhint=(EditText)findViewById(R.id.securityhint_edt);
register=(Button)findViewById(R.id.register_btn);
cancel=(Button)findViewById(R.id.cancel_btn);
check=(CheckBox)findViewById(R.id.checkBox1);

check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

if(!isChecked)
{
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});

register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String Pass=password.getText().toString();
String Secu=securityhint.getText().toString();
String Repass=repassword.getText().toString();
String nam=na.getText().toString();



if(Pass.equals("")||Repass.equals("")||Secu.equals("")||nam.equals(""))
{
Toast.makeText(getApplicationContext(), "Fill All Fields", Toast.LENGTH_LONG).show();
return;
}

if(!Pass.equals(Repass))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(Pass, Repass,Secu,nam);

// reg_btn.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Log.d("PASSWORD",Pass);
Log.d("RE PASSWORD",Repass);
Log.d("SECURITY HINT",Secu);
Log.d("NAME",nam);


Intent i=new Intent(Registration.this,MainActivity.class);
startActivity(i);
}
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ii=new Intent(Registration.this,MainActivity.class);
startActivity(ii);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.registration, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Reply
avatar
Nouha
AUTHOR
March 3, 2015 at 3:31 AM delete


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
LoginDataBaseAdapter loginDataBaseAdapter;
Button login;
Button registerr;
EditText enterpassword,name;
TextView forgetpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Login");
login=(Button)findViewById(R.id.login_btn);
registerr=(Button)findViewById(R.id.register_btn);
enterpassword=(EditText)findViewById(R.id.password_edt);
name=(EditText)findViewById(R.id.nam_edt);
forgetpass=(TextView)findViewById(R.id.textView2);
loginDataBaseAdapter = new LoginDataBaseAdapter(getApplicationContext());
loginDataBaseAdapter.open();

registerr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,Registration.class);
startActivity(i);
}
});

login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Password=enterpassword.getText().toString();
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(Password);
String nam=name.getText().toString();
String storednam=loginDataBaseAdapter.getSinlgeEntry(nam);
if(Password.equals(storedPassword) || nam.equals(storednam) )
{
Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_LONG).show();
Intent ii=new Intent(MainActivity.this,Home.class);
startActivity(ii);
}
else
if(Password.equals("") ){
Toast.makeText(MainActivity.this, "Please Enter Your Password ", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "Incorrect Password ", Toast.LENGTH_LONG).show();
}
}
});

forgetpass.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

final Dialog dialog = new Dialog(MainActivity.this);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.forget_search);
dialog.show();

final EditText security=(EditText)dialog.findViewById(R.id.securityhint_edt);
final TextView getpass=(TextView)dialog.findViewById(R.id.textView3);

Button ok=(Button)dialog.findViewById(R.id.getpassword_btn);
Button cancel=(Button)dialog.findViewById(R.id.cancel_btn);

ok.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String userName=security.getText().toString();
if(userName.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your securityhint", Toast.LENGTH_SHORT).show();
}
else
{
String storedPassword=loginDataBaseAdapter.getAllTags(userName);
if(storedPassword==null)
{
Toast.makeText(getApplicationContext(), "Please enter correct securityhint", Toast.LENGTH_SHORT).show();
}else{
Log.d("GET PASSWORD",storedPassword);
getpass.setText(storedPassword);
}
}
}
});
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

dialog.show();
}
});
}

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

Reply
avatar
Nouha
AUTHOR
March 3, 2015 at 3:31 AM delete

can you help me i don't now the problem

Reply
avatar
Kalidoss
AUTHOR
March 6, 2015 at 1:40 PM delete

Hi nouha,

Sorry for late reply,use following methods to check the login process after you have to insert both username and password in sqlite database

String userName=username_edittext.getText().toString();
String password=password_edittext.getText().toString();


if(userName.equals("")&&password.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your username and password",Toast.LENGTH_SHORT).show();
}
else
{
String Password=loginDataBaseAdapter.getAllTags(userName);
if(Password!=null)
{
Log.d("GET PASSWORD",Password);
if(Password.equals(password))
{
Toast.makeText(getApplicationContext(), "Login Successfully...",Toast.LENGTH_SHORT).show();
}
}

i hope it will be helpful to you ...

Reply
avatar
Unknown
AUTHOR
May 7, 2015 at 1:07 AM delete

How to change password??

Reply
avatar
Kalidoss
AUTHOR
May 7, 2015 at 11:38 AM delete

Hi kaur,
Need to create additional methods for change password like account update method

Reply
avatar
Unknown
AUTHOR
June 10, 2015 at 11:09 AM delete

Hi,
I want to create multiple table in ur app(Login and Registration using Sqlite Database with Forget Password).Can u plz help me ? how to create and connect with database?

Reply
avatar
Kalidoss
AUTHOR
June 10, 2015 at 12:18 PM delete

Hi Pavithra,

In this project i have create one table for store login credentials if you need multiple table means create table in LoginDataBaseAdapter.class

Like this:

static final String DATABASES_NAME = "folder.db";
static final String DATABASE_CREATE = "create table "+"FOLDER"+
"( " +"ID integer primary key autoincrement,"+ "FOLDERNAME text,"+"ORIGINALPATH text,"+ "HIDEPATH text,"+ "FOLDERNAMEWITHDOT text,"+ "PATH text,"+ "HIDE integer) ";

initialize two tables

public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
dbHelper = new DataBaseHelper(context, DATABASES_NAME , null, DATABASE_VERSION);
}

and then create,update,delete methods are same like above table methods.

Reply
avatar
Tin Tran
AUTHOR
June 14, 2015 at 8:14 PM delete

Thank you Mr.Kalidoss so much! It' s really useful for me to learn. From your direction above; I can improve my login form conveniently. Thank you again (=^_^=) !

Reply
avatar
October 25, 2015 at 6:16 PM delete

this post is very useful for every developer.

Reply
avatar
Unknown
AUTHOR
October 31, 2015 at 2:50 PM delete

Thank you Mr.Kalidoss, it's really useful for me.

Reply
avatar
Unknown
AUTHOR
December 29, 2015 at 6:32 PM delete

hello kalidoss

I'm having errors in "R", so what should i do to fix it.

all classes contain R send errors to me

Reply
avatar
Anonymous
AUTHOR
December 29, 2015 at 7:30 PM delete

i have a problem in R

errors occur in every class contains R

why do you think this happens ?

Reply
avatar
Kalidoss
AUTHOR
December 30, 2015 at 11:13 AM delete

check your layout files

if any error occurs in your layout files its show error in "R"

Reply
avatar
Anonymous
AUTHOR
January 30, 2016 at 5:50 PM delete

Can you combine DataBaseHelper.java and LoginDataBaseAdapter.java into one class? Please help

Reply
avatar
Guru
AUTHOR
February 17, 2016 at 1:10 PM delete

I am using android studio 1.5.1 with api 23 this will suppor ur code

Reply
avatar
Kalidoss
AUTHOR
February 17, 2016 at 7:44 PM delete

No, actually i have used eclipse to develop this project.

Reply
avatar
Unknown
AUTHOR
March 18, 2016 at 9:02 PM delete

i also want to add a filed of username but i also get problems
please if someone successfully send me the code to this email
khan_samsor@yahoo.com

Reply
avatar
Unknown
AUTHOR
April 4, 2016 at 6:09 PM delete

hlo
plz snd me the login and
password which u enter
o signup

Reply
avatar
Anonymous
AUTHOR
April 6, 2016 at 7:47 PM delete

HI.here is the code for login with username and password.

public class LoginDataBaseAdapter {

static final String DATABASE_NAME = "RestaurantDB";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 4;

static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID integer primary key autoincrement,"+"USERNAME,"+ "PASSWORD text,"+"REPASSWORD text,"+ "SECURITYHINT text) ";

public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;

public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}

public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String username,String password,String repassword,String securityhint)
{
ContentValues newValues = new ContentValues();
newValues.put("USERNAME",username);
newValues.put("PASSWORD", password);
newValues.put("REPASSWORD",repassword);
newValues.put("SECURITYHINT",securityhint);

db.insert("LOGIN", null, newValues);
}

public int deleteEntry(String password)
{
String where="PASSWORD=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{password}) ;
return numberOFEntriesDeleted;
}

public String getSinlgeEntry(String password)
{
Cursor cursor=db.query("LOGIN", null, " PASSWORD=?", new String[]{password}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String repassword= cursor.getString(cursor.getColumnIndex("REPASSWORD"));
cursor.close();
return repassword;
}

//declare single username
public String getSingleEntryUsername(String username){
Cursor cursor=db.query("LOGIN",null,"USERNAME=?",new String[]{username},null,null,null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String uname= cursor.getString(cursor.getColumnIndex("USERNAME"));
cursor.close();
return uname;

}

public String getAllTags(String a) {


Cursor c = db.rawQuery("SELECT * FROM " + "LOGIN" + " where SECURITYHINT = '" +a + "'" , null);
String str = null;
if (c.moveToFirst()) {
do {
str = c.getString(c.getColumnIndex("PASSWORD"));
} while (c.moveToNext());
}
return str;
}


public void updateEntry(String username,String password,String repassword)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("USERNAME",username);
updatedValues.put("PASSWORD", password);
updatedValues.put("REPASSWORD",repassword);
updatedValues.put("SECURITYHINT",repassword);

String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{password});
}



public HashMap getAnimalInfo(String id) {
HashMap wordList = new HashMap();
String selectQuery = "SELECT * FROM LOGIN where SECURITYHINT='"+id+"'";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
wordList.put("PASSWORD", cursor.getString(1));
} while (cursor.moveToNext());
}
return wordList;
}
}


Reply
avatar
Anonymous
AUTHOR
April 6, 2016 at 7:50 PM delete

Hi Kalidoss...thanks alot...its worked

Reply
avatar
Naman
AUTHOR
May 24, 2016 at 7:14 PM delete

boss, I really liked your code and I believe it is worth. But, unfortunately, in my case, I am working on my own database i.e. localhost with php. So can you help me in getting security hint from there and displaying in the popup window.

Reply
avatar
Naman
AUTHOR
May 24, 2016 at 7:25 PM delete

boss, I really liked your code and I believe it is worth. But, unfortunately, in my case, I am working on my own database i.e. localhost with php. So can you help me in getting security hint from there and displaying in the popup window.

Reply
avatar
Unknown
AUTHOR
September 18, 2016 at 5:13 PM delete

can not store data in storePassword in variable when i fetch data from database help me


String userName = edtEmailid.getText().toString();
String password = edtPassword.getText().toString();


String storedPassword = loginDatabaseHelper.getSinlgeEntry(userName);// problems here...not store data in storedPassword variable

if (password.equals(storedPassword)){

Toast.makeText(getApplication(), "Sign In Successfully", Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(getApplication(), "Email Id or Password does not match", Toast.LENGTH_SHORT).show();

}

Reply
avatar
Unknown
AUTHOR
April 1, 2017 at 12:40 PM delete

Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

Reply
avatar
Unknown
AUTHOR
May 3, 2017 at 11:21 PM delete

hi ! plz helpe me the password is not insert in table why ?

Reply
avatar
June 26, 2017 at 1:07 PM delete

This is one of the best programming blogs which I have ever come across. In fact, the details in the blog are so simplified and that motivates me to be part of this educative forum. Through professional Science Homework Writing Aid you can be assisted to generate such projects.

Reply
avatar