Write a Program on Database with Constraint Layout using Android Studio 3.0.1 ?

INTRODUCTION :

Hii everyone, here we are going to construct a program on database using Constraint Layout.Before moving to the datails of the code it is better to know about constant layout.Constraint Layout the latest platform for the designing in android studio that gives us more flexibility to construct a layout, the advantage that it adjust the layout in the portrait mode and in the landscape mode as well.

DESCRIPTION OF CODE:

Moving to the code first of all, we will construct an another class and extend the SQLiteOpenHelper, this is a class which is used to store the data and then make a constructor.Moving to the mainactivity program we will make an object of the class named as DatabaseHelper and then we call it in the oncreated function and call it in the writable mode.And then we have four buttons i.e. ADD, SEARCH, CHANGE and DELETE and these buttons we will make one function for one button.

DESIGNING OF THE SCREEN:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#aaffbb"
    android:layout_height="match_parent"
    tools:context="com.example.sakshi.studentconstrantuse.MainActivity">

    <TextView
        android:id="@+id/tvMessages"
        android:layout_width="523dp"
        android:layout_height="43dp"
        android:layout_marginEnd="172dp"
        android:layout_marginRight="172dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:background="#5577ff"
        android:hint="Text Message"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.021"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="123dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"
        android:background="#aaff99bb"
        android:hint="NAME"
        app:layout_constraintStart_toStartOf="@+id/tvMessages"
        app:layout_constraintTop_toBottomOf="@+id/tvMessages" />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="177dp"
        android:layout_height="45dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:background="#aacc"
        android:ems="10"
        android:hint="Enter NAME"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="@+id/tvMessages"
        app:layout_constraintHorizontal_bias="0.221"
        app:layout_constraintStart_toEndOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/tvMessages" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="114dp"
        android:layout_height="49dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:background="#aaff99bb"
        android:text="ROLL NUMBER"
        app:layout_constraintBottom_toBottomOf="@+id/txtRollNumber"
        app:layout_constraintEnd_toStartOf="@+id/txtRollNumber"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/txtRollNumber"
        android:layout_width="182dp"
        android:layout_height="47dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="36dp"
        android:background="#aacc"
        android:ems="10"
        android:hint="Enter ROLL NUMBER"
        android:inputType="numberSigned"
        app:layout_constraintEnd_toEndOf="@+id/txtName"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/txtName"
        app:layout_constraintTop_toBottomOf="@+id/txtName" />

    <Button
        android:id="@+id/bttnAdd"
        android:layout_width="79dp"
        android:layout_height="46dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="60dp"
        android:background="#aa1199"
        android:text="ADD"
        android:onClick="doAdd"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView7"
        app:layout_constraintTop_toBottomOf="@+id/textView7"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:onClick="doDelete"
        android:id="@+id/bttnDelete"
        android:layout_width="74dp"
        android:layout_height="44dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="60dp"
        android:background="#aa1199"
        android:text="DELETE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/bttnAdd"
        app:layout_constraintTop_toBottomOf="@+id/textView7"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:onClick="doChange"
        android:id="@+id/bttnChange"
        android:layout_width="77dp"
        android:layout_height="46dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="60dp"
        android:background="#aa1199"
        android:text="CHANGE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/bttnDelete"
        app:layout_constraintTop_toBottomOf="@+id/txtRollNumber"
        app:layout_constraintVertical_bias="0.016" />

    <Button
        android:onClick="doSearch"
        android:id="@+id/bttnSearch"
        android:layout_width="81dp"
        android:layout_height="46dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:background="#aa1199"
        android:text="Search"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/txtRollNumber"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/bttnChange"
        app:layout_constraintTop_toBottomOf="@+id/txtRollNumber"
        app:layout_constraintVertical_bias="0.005" />


</android.support.constraint.ConstraintLayout>

MainActivity CODE :

package com.example.sakshi.studentconstrantuse;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView tvMessages;
private EditText txtName,txtRollNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtName=(EditText)findViewById(R.id.txtName);
        txtRollNumber=(EditText)findViewById(R.id.txtRollNumber);
        tvMessages=(TextView)findViewById(R.id.tvMessages);
        try
        {
            DatabaseHelper helper=new DatabaseHelper(this);
            SQLiteDatabase database=helper.getWritableDatabase();
            database.execSQL("create table student(rollno integer primary key,name text)");
            tvMessages.setText("Created");
            database.close();
        }
        catch (Exception ex)
        {
            System.out.println(ex);
           // tvMessages.setText(ex.getMessage());
        }
    }
    public void doAdd(View view)
    {
        try
        {
            DatabaseHelper helper=new DatabaseHelper(this);
            SQLiteDatabase database=helper.getWritableDatabase();
            ContentValues values=new ContentValues();//is used to store the data
            values.put("rollno",Integer.parseInt("" + txtRollNumber.getText()));
            values.put("name","" + txtName.getText());
           int n= (int)database.insert("student",null,values);//it tells the no. of data inserted
           tvMessages.setText("Records Inserted = " + n );
        }
        catch (Exception ex) {
            System.out.println(ex);
            tvMessages.setText(ex.getMessage());
        }
    }
    public void doDelete(View view)
    {
        try
        {
            DatabaseHelper helper=new DatabaseHelper(this);
            SQLiteDatabase database=helper.getWritableDatabase();
           String[] parameters={"" + txtRollNumber.getText()};
            int n= (int)database.delete("student","rollno=?",parameters);
            tvMessages.setText("Records Deleted = " + n );
        }
        catch (Exception ex) {
            System.out.println(ex);
            tvMessages.setText(ex.getMessage());
        }
    }
    public void doChange(View view)
    {
        DatabaseHelper helper=new DatabaseHelper(this);
        SQLiteDatabase database=helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name","" + txtName.getText());
        String[] parameters={"" + txtRollNumber.getText()};
        int n= (int)database.update("student",values ,"rollno=?",parameters);
        tvMessages.setText("Records Updated = " + n );
    }
    public void doSearch(View view)
    {
        try
        {
            DatabaseHelper helper=new DatabaseHelper(this);
            SQLiteDatabase database=helper.getReadableDatabase();
            String[] columns={"name"};
            String[] parameters={"" + txtRollNumber.getText()};
            Cursor cursor=database.query("student",columns,"rollno=?",parameters,null,null,null);
            if(cursor.moveToFirst())
            {
                String name=cursor.getString(0);
                txtName.setText(name);
            }
            else
                txtName.setText("Not found");
            tvMessages.setText("Search Done");
        }
        catch (Exception ex) {
            System.out.println(ex);
            tvMessages.setText(ex.getMessage());
        }
    }

}

DatabaseHelper Class :

package com.example.sakshi.studentconstrantuse;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper
{

    public DatabaseHelper(Context context) {
        super(context, "testdb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

OUTPUT :

Hope You Find It Helpful…Thank You!

Leave a Reply