In every Android application we need to save some details in the
Android device. For this there are some options that the Android Operating system provides and those are SQL, File and SharedPreference. Here, I am going to describe how you can store and retrieve data from Database in Android. For this, I am using two Activities, one activity contains the Login Page where you can check whether user data exist in the database or not. In RegisterActivity, the user can store his/her details in the database.
For the first one, create the login Activity layout,
- <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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Login Here"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- <LinearLayout android:layout_width="match_parent"
- android:layout_below="@+id/textView"
- android:id="@+id/userNameLayout"
- android:layout_margin="10dp"
- android:orientation="horizontal"
- android:layout_height="50dp">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="User Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:layout_weight="2"
- android:id="@+id/username"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout android:layout_width="match_parent"
- android:layout_below="@+id/userNameLayout"
- android:layout_margin="10dp"
- android:orientation="horizontal"
- android:layout_height="50dp"
- android:id="@+id/linearLayout">
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Password :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/password"
- android:inputType="textPassword"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Login"
- android:id="@+id/loginButton"
- android:layout_below="@+id/linearLayout"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register User"
- android:id="@+id/registerButton"
- android:layout_below="@+id/loginButton"
- android:layout_centerHorizontal="true" />
-
- </RelativeLayout>
This contains two edit texts where the user can enter their user name and password. This contains two buttons: Login and Register. On clicking the Login button we need to check the database so that the user details exists or not. Another button named Register will call the Register Activity.
Now create the Registration layout also,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Register Here"
- android:id="@+id/textView2"
- android:layout_gravity="center_horizontal" />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
-
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/name"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
-
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="User Name :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/userName"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal">
-
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="Password :"
- android:layout_height="wrap_content" />
- <EditText
- android:layout_width="0dp"
- android:id="@+id/Password"
- android:inputType="textPassword"
- android:layout_weight="2"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register"
- android:id="@+id/registerButton"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
Now comes the main part that is the Database helper class. In Android there is a class named SQLiteOpenHelper which is an abstract class and is used for the data base operations. Firstly, create a class that extends from SQLiteOpenHelper and implement the onCreate() and onUpgrade().
- onCreate() - It is used for creating the data base.
- onUpgrade() - It is used for upgrading the database if there is a new version available for the database. Please see the class in detail,
- public class DBHelper extends SQLiteOpenHelper
- {
- private static final int VERSION = 1;
- private static final String DATA_BASE_NAME = "users.db";
- private static final String TABLE_NAME = "login";
- private static final String COLUMN_ID = "id";
- private static final String COLUMN_NAME = "name";
- private static final String COLUMN_USER_NAME = "username";
- private static final String COLUMN_PASSWORD = "password";
- private static final String CREATE_TABLE = "create table " + TABLE_NAME + " (" + COLUMN_ID + " integer primary key not null, " + COLUMN_NAME + " text not null," + COLUMN_USER_NAME + " text not null, " + COLUMN_PASSWORD + " text not null);";
- private SQLiteDatabase db = null;
- public DBHelper(Context context) {
- super(context, DATA_BASE_NAME, null, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase sqLiteDatabase) {
- sqLiteDatabase.execSQL(CREATE_TABLE);
- db = sqLiteDatabase;
- }
- public void registerUser(UserDetails userDetails) {
- db = this.getWritableDatabase();
- ContentValues contentValues = new ContentValues();
- contentValues.put(COLUMN_NAME, userDetails.getName());
- contentValues.put(COLUMN_PASSWORD, userDetails.getPassword());
- contentValues.put(COLUMN_USER_NAME, userDetails.getUserName());
- db.insert(TABLE_NAME, null, contentValues);
- db.close();
- }
- public boolean isUserExists(UserDetails userDetails) {
- db = this.getReadableDatabase();
- String querry = "select * from " + TABLE_NAME + " where " + COLUMN_USER_NAME + "='" + userDetails.getUserName() + "' and " + COLUMN_PASSWORD + "='" + userDetails.getPassword() + "'";
- Cursor cursor = db.rawQuery(querry, null);
- if (cursor.moveToFirst()) {
- return true;
- }
- return false;
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {}
- }
There are two methods, registerUser() and isUserExists() both are interacting with our two Activities for database operations. We also create one model class for setting and getting the details,
- public class UserDetails
- {
- private String userName = null;
- private String password = null;
- private String name = null;
- public String getUserName()
- {
- return userName;
- }
- public void setUserName(String userName)
- {
- this.userName = userName;
- }
- public String getPassword()
- {
- return password;
- }
- public void setPassword(String password)
-
- {
- this.password = password;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
In the LoginActivity button click use this code to check the user data exists in the table or not by using,
- if (!TextUtils.isEmpty(userName.getText().toString()) && !TextUtils.isEmpty(password.getText().toString())) {
- UserDetails userDetails = new UserDetails();
- userDetails.setPassword(password.getText().toString());
- userDetails.setUserName(userName.getText().toString());
- DBHelper dbHelper = new DBHelper(MainActivity.this);
- if(dbHelper.isUserExists(userDetails))
- Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(MainActivity.this, "Invalid login", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(MainActivity.this, "please enter all the fields", Toast.LENGTH_SHORT).show();
- }
And in the Register activity for inserting the table we can use the following code,
- if (!TextUtils.isEmpty(userName.getText().toString()) && !TextUtils.isEmpty(password.getText().toString()) && !TextUtils.isEmpty(name.getText().toString())) {
- UserDetails userDetails = new UserDetails();
- userDetails.setName(name.getText().toString());
- userDetails.setPassword(password.getText().toString());
- userDetails.setUserName(userName.getText().toString());
- DBHelper dbHelper = new DBHelper(RegisterActivity.this);
- dbHelper.registerUser(userDetails);
- Toast.makeText(RegisterActivity.this, "Successfully registered", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(RegisterActivity.this, "please enter all the fields", Toast.LENGTH_SHORT).show();
- }
In all the db operations user will be intimated by a toast message as status.
Here are the screenshots,
Read more articles on Android: