This is a good example of login and Sign up page using SQLite.
In this project, we have 3 activities and 2 java classes.
Contact.java
DatabaseHelper.java
Display.java
MainActivity.java
SignUp.java
Contact.java
In this project, we have 3 activities and 2 java classes.
Contact.java
DatabaseHelper.java
Display.java
MainActivity.java
SignUp.java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blogspot.csdevbin.sqliteexample.MainActivity">
<EditText
android:id="@+id/editText_u"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="91dp"
android:hint="Username" />
<EditText
android:id="@+id/editText_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_u"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:onClick="onSignIn"
android:text="Log in" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button"
android:layout_marginTop="48dp"
android:text="Sign up" />
</RelativeLayout>
|
activity_signup.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blogspot.csdevbin.sqliteexample.SignUp">
<EditText
android:id="@+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="58dp"
android:hint="Name" />
<EditText
android:id="@+id/editText_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_name"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editText_uname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_email"
android:hint="Username" />
<EditText
android:id="@+id/editText_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_uname"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/editText_confirmpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_pass"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_confirmpass"
android:layout_marginTop="50dp"
android:onClick="onSignUp"
android:text="Sign up" />
</RelativeLayout>
|
activity_display
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.blogspot.csdevbin.sqliteexample.Display">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="57dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
|
package com.blogspot.csdevbin.sqliteexample;
/** * Created by DeskTop on 4/21/2016. */
public class Contact {
int id;
String name, email, uname, pass;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setEmail(String email){
this.email=email;
}
public String getEmail(){
return this.email;
}
public void setUname(String uname){
this.uname=uname;
}
public String getUname(){
return this.uname;
}
public void setPass(String pass){
this.pass=pass;
}
public String getPass(){
return this.pass;
}
}
|
DatabaseHelper.java
package com.blogspot.csdevbin.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contact.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts
(id integer primary key not null ," +
"name text not null , email text not null
,uname text not null ,pass text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertContact(Contact c) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query ="select * from contacts";
Cursor cursor = db.rawQuery(query,null);
int count =cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_NAME, c.getName());
values.put(COLUMN_EMAIL, c.getEmail());
values.put(COLUMN_UNAME, c.getUname());
values.put(COLUMN_PASS, c.getPass());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String uname) {
db = this.getReadableDatabase();
String query = "select uname, pass from " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()) {
do {
a = cursor.getString(0);
if (a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
|
MainActivity.java
package
com.blogspot.csdevbin.sqliteexample;
import
android.content.Intent;
import
android.opengl.EGLDisplay;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
public
class MainActivity extends AppCompatActivity {
DatabaseHelper helper = new
DatabaseHelper(this);
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)
findViewById(R.id.button2);
b.setOnClickListener(new
View.OnClickListener() {
@Override public void onClick(View v) {
Intent in = new
Intent(MainActivity.this, SignUp.class);
startActivity(in);
}
});
}
public void onSignIn(View v) {
if (v.getId() == R.id.button) {
EditText u = (EditText)
findViewById(R.id.editText_u);
String str =
u.getText().toString();
EditText p = (EditText)
findViewById(R.id.editText_p);
String pass =
p.getText().toString();
String password =
helper.searchPass(str);
if (pass.equals(password)) {
Intent in = new Intent(MainActivity.this,
Display.class);
in.putExtra("Username", str);
startActivity(in);
} else {
Toast.makeText(MainActivity.this, "Username and Password
doesn't
matched", Toast.LENGTH_SHORT)
.show();
}
}
}
}
|
SignUp.java
package
com.blogspot.csdevbin.sqliteexample;
import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.Toast;
public
class SignUp extends AppCompatActivity {
DatabaseHelper helper = new
DatabaseHelper(this);
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
}
public void onSignUp(View v) {
if (v.getId() == R.id.button3) {
EditText name = (EditText)
findViewById(R.id.editText_name);
EditText email = (EditText)
findViewById(R.id.editText_email);
EditText uname = (EditText)
findViewById(R.id.editText_uname);
EditText pass = (EditText)
findViewById(R.id.editText_pass);
EditText confirmpass = (EditText)
findViewById(R.id.editText_confirmpass);
String namestr =
name.getText().toString();
String emailstr =
email.getText().toString();
String unamestr =
uname.getText().toString();
String passstr = pass.getText().toString();
String confirmpassstr =
confirmpass.getText().toString();
if
(!passstr.equals(confirmpassstr)) {
Toast.makeText(SignUp.this,
"Password doesn't matched", Toast.LENGTH_SHORT)
.show();
} else {
Intent in = new
Intent(SignUp.this, MainActivity.class);
Contact c = new Contact();
c.setName(namestr);
c.setEmail(emailstr);
c.setUname(unamestr);
c.setPass(passstr);
helper.insertContact(c);
Toast.makeText(SignUp.this,"Sign up
successful",Toast.LENGTH_SHORT).show();
startActivity(in);
}
}
}
}
|
Display.java
package
com.blogspot.csdevbin.sqliteexample;
import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.TextView;
public
class Display extends AppCompatActivity {
@Override protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
TextView tv =
(TextView)findViewById(R.id.textView);
Intent in =getIntent();
String str
=in.getStringExtra("Username");
tv.setText(str);
}
}
|
Comments :
Post a Comment