Android App Development With AndroidCource: April 2018

Sunday, 29 April 2018

Alarm Manager And Broadcast Receiver Example in android

What is Broadcast Receiver ?

A broadcast receiver (receiver) is an Android component which allows to register for system or application events.And All registered receivers for an event are notified by the Android runtime once this event happens.Android apps can send or receive broadcast messages from the Android system and other Android apps.A Broadcast Receiver is Runtime Issue in System and Notify System. For Example. Your Mobile is Fully Charged or Low bettary or Downloaded File to Notify to send Broadcast in system apps. and display notification to low bettary etc. Brodcast Receiver is Implement to add register in AndroidManifest file and You can also Dynamic add to broadcast receiver.

What is Alarm Manager ?

Android AlarmManager allows you to access system alarm.u can schedule your application to run at a specific time in the future. It works whether your phone is running or not.

Register Broadcast Receiver AndroidManifest.xml file

<receiver android:name=".AlarmReceiver" />

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.jobscheduledemo.MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="20dp"
        android:inputType="textPersonName"
        android:hint = "Enter Second "
        android:id="@+id/tv_time"/>

    <Button
        android:id="@+id/btn_startjob"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="Start Alarm"
        android:textAllCaps="false" />

  
</LinearLayout>

MainActivity.javal

package com.example.bhaumik.jobscheduledemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    EditText time;

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

        time = findViewById(R.id.tv_time);
       
    jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);*/

        findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setAlarm();

       
                Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show();

            }
        });
        
       
    }

    private void setAlarm() {

        int sec = Integer.parseInt(time.getText().toString());

        Intent intent=new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0, intent,0);
        AlarmManager a=(AlarmManager)getSystemService(ALARM_SERVICE);
                a.set(AlarmManager.RTC,System.currentTimeMillis() +sec*1000,pendingIntent);
        Toast.makeText(getApplicationContext(),"Alarm set",Toast.LENGTH_LONG).show();

        
    }
}

AlarmReceiver.java

package com.example.bhaumik.jobscheduledemo;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;


public class AlarmReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {


        Toast.makeText(context,"Times up",Toast.LENGTH_LONG).show();

        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle("Alarm Notify")
                .setContentText("This is Alarm Notification")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0,builder.build());


    }
}

Output


    Add caption

Wednesday, 25 April 2018

Background Job Scheduler Example in android

How to Create Background Process in Job Scheduler Start Job And Stop Job in android example


What is Job Scheduler in Android


JobScheduler is a Android framework API for scheduling tasks or work. It was first included available in Android 5.0 (API level 21), and remains under active development.JobScheduler shoulb be define in JobService.Your JobService is actually going to be a Service that extends the JobService class. This is what enables the system to perform your work for you, regardless of whether your app is active.


Manifest.xml File To Add Job Service.


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<service android:name=".MyJobSchedular"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="true"
            android:enabled="true"/>

MainActivity.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dipl14.jobscheduledemo.MainActivity">

    <Button
        android:id="@+id/btn_startjob"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Start Job"
        android:textAllCaps="false"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn_stopJob"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="Stop Job"
        android:textAllCaps="false"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>
</LinearLayout>

Create Object of JobSchedule And Create Component Name And JobInfo Object.Job info is a Container of data passed to the JobScheduler parameters required to schedule work against the calling application. These are constructed using the JobInfo.Builder . You must specify at least one sort of constraint on the JobInfo object that you are creating.


MainActivity.java


package com.example.dipl14.jobscheduledemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final int JOB_ID = 101;

    public JobScheduler jobScheduler;
    public JobInfo jobInfo;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setAlarm();
        ComponentName componentName = new ComponentName(MainActivity.this,MyJobSchedular.class);
        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);

        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setPeriodic(5000);
        jobInfo = builder.build();
        jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

        findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //  setAlarm();

                jobScheduler.schedule(jobInfo);

                
                Toast.makeText(MainActivity.this, "Start Job", Toast.LENGTH_SHORT).show();

            }
        });
        
        findViewById(R.id.btn_stopJob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                jobScheduler.cancelAll();

                Toast.makeText(MainActivity.this, "Stop Job", Toast.LENGTH_SHORT).show();
            }
        });
    }

 
}

Create MyJobService file to extends JobService and Implements method to OnStartJob And OnStopJob.OnStartJob to continue to running in background Process to Start Job And Cancel Or Stop Job to Cancelled Job with background process.


MyJobService.java



package com.example.dipl14.jobscheduledemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static final int JOB_ID = 101;

    public JobScheduler jobScheduler;
    public JobInfo jobInfo;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setAlarm();
        ComponentName componentName = new ComponentName(MainActivity.this,MyJobSchedular.class);
        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);

        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);
        builder.setPeriodic(5000);
        jobInfo = builder.build();
        jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

        findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //  setAlarm();

                jobScheduler.schedule(jobInfo);

                
                Toast.makeText(MainActivity.this, "Start Job", Toast.LENGTH_SHORT).show();

            }
        });
        
        findViewById(R.id.btn_stopJob).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                jobScheduler.cancelAll();

                Toast.makeText(MainActivity.this, "Stop Job", Toast.LENGTH_SHORT).show();
            }
        });
    }

 
}

Output



Sunday, 22 April 2018

How to Create Sqlite Database with crud Operation (Insert,Update,Delete,Search) in android.

Create Simple Contact Demo in Sqlite Database with Insert,Update,Delete And Search Operation.In This Code to How to Create Database in Sqlite And How to Add Delete Update And Search Demo in Android.

Create Database Helper File to Create or Upgrade Database

DatabaseHelper.java


 

package com.example.bhaumik.sqldemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Bhaumik on 5/29/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "infos";
    private static final String ID = "ID";
    private static final String NAME = "NAME";
    private static final String EMAIL = "EMAIL";
    private static final String MOBILE_NO = "MOBILENO";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
        + NAME + " TEXT, " + EMAIL + " TEXT, " + MOBILE_NO + " TEXT );");

        Log.d("Database : -> ","Database Create/Open...");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
    }

    public boolean InsertData(String name,String email,String mobile)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME,name);
        contentValues.put(EMAIL,email);
        contentValues.put(MOBILE_NO,mobile);
        long result = db.insert(TABLE_NAME,null,contentValues);

        if(result == -1)
            return false;
        else
            return true;
    }

    public Cursor SearchData(String mobile)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + MOBILE_NO + " = '" + mobile + "'";

        Cursor cursor = db.rawQuery(query,null);

        return cursor;
    }

    public void UpdateData(String mobiles,String name,String email)
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME,name);
        contentValues.put(EMAIL,email);

        db.update(TABLE_NAME,contentValues,MOBILE_NO + " = '" + mobiles + "'",null);

    }

    public void DeleteData(String mobile)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_NAME , MOBILE_NO + " = '" + mobile + "'",null);

    }
}

Create MainActivity File to Handle Add,Update,Delete And Search Event to Open Another Activity.

MainActivity.xml



	<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
    android:orientation="vertical"

    android:background="@color/colorAccent"
    tools:context="com.example.bhaumik.sqldemo.MainActivity">


    <Button
        android:id="@+id/btn_add_contact"
        style="@style/ButtonStyle"
        android:text="Add Contacts" />

    <Button
        android:id="@+id/btn_update_contact"
        style="@style/ButtonStyle"
        android:text="Update Contacts" />

    <Button
        android:id="@+id/btn_delete_contact"
        style="@style/ButtonStyle"
        android:text="Delete Contacts" />

    <Button
        android:id="@+id/btn_search_contact"
        style="@style/ButtonStyle"
        android:text="Search Contacts" />

</LinearLayout>

Create Code file to open Actiivity to Add,Update,Delete And Search Button Event.

MainActivity.Java



package com.example.bhaumik.sqldemo;

import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;

public class MainActivity extends AppCompatActivity{

    Button add,update,delete,search;

    DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());
        setContentView(R.layout.activity_main);

        databaseHelper = new DatabaseHelper(this);

        init();

        setListener();
    }

    private void init() {

        add = (Button) findViewById(R.id.btn_add_contact);
        update = (Button) findViewById(R.id.btn_update_contact);
        delete = (Button) findViewById(R.id.btn_delete_contact);
        search = (Button) findViewById(R.id.btn_search_contact);

    }

    private void setListener() {

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this,AddContactActivity.class);
                startActivity(intent);
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,UpdateContactActivity.class);
                startActivity(intent);
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,DeleteContactActivity.class);
                startActivity(intent);
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SearchContactActivity.class);
                startActivity(intent );
            }
        });
    }
}

Design For Add,Update,Delete And Search Activity XMl File.To Open Activity when button click event.

addcontact_activity.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    tools:context="com.example.bhaumik.sqldemo.AddContactActivity">

    <EditText
        android:id="@+id/et_name"
        style="@style/EditTextStyle"
        android:textColorHint="@color/WhiteColor"
        android:hint="Enter Name"/>
    <EditText
        android:id="@+id/et_email"
        style="@style/EditTextStyle"
        android:textColorHint="@color/WhiteColor"
        android:hint="Enter Email"/>
    <EditText
        android:id="@+id/et_mobile"
        style="@style/EditTextStyle"
        android:hint="Enter Mobile No"
        android:inputType="phone"
        android:textColorHint="@color/WhiteColor"
        android:imeOptions="actionDone"/>

    <Button
        android:id="@+id/btn_add"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:text="Add "
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:background="@color/ButtonColor"
        android:textColor="@color/WhiteColor"
        android:textSize="18dp"
        />
</LinearLayout>

updatecontact_activity.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.sqldemo.UpdateContactActivity">


    <EditText
        style="@style/EditTextStyle"
        android:id="@+id/et_update_mobile"
        android:hint="Enter Mobile No"
        android:inputType="phone"
        android:textColor="@color/ButtonColor"/>

    <Button
        style="@style/ButtonStyle"
        android:id="@+id/btn_search_updates"
        android:text="Search"/>

    <EditText
        style="@style/EditTextStyle"
        android:id="@+id/et_update_name"
        android:hint="Your Name"
        android:textColor="@color/ButtonColor"/>
    <EditText
        style="@style/EditTextStyle"
        android:id="@+id/et_update_email"
        android:hint="Your Email"
        android:textColor="@color/ButtonColor"/>
    <Button
        style="@style/ButtonStyle"
        android:id="@+id/btn_update"
        android:text="Update"/>
</LinearLayout>

deletecontact_activity.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.sqldemo.DeleteContactActivity">

    <EditText
        style="@style/EditTextStyle"
        android:id="@+id/et_delete_mobile"
        android:textColor="@color/ButtonColor"/>

    <Button
        style="@style/ButtonStyle"
        android:id="@+id/btn_delete"
        android:text="Delete"/>
</LinearLayout>

searchcontact_activity.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.sqldemo.SearchContactActivity">

    <EditText
       style="@style/EditTextStyle"
        android:id="@+id/et_search_mobile"
        android:hint="Enter Mobile No"
        android:textColor="@color/ButtonColor"
        android:inputType="phone"
        />

    <Button
       style="@style/ButtonStyle"
        android:id="@+id/btn_search"
        android:text="Search"/>

    <TextView
        style="@style/TextViewStyle"
        android:id="@+id/tv_search_name"
        />
    <TextView
       style="@style/TextViewStyle"
        android:id="@+id/tv_search_email"/>
</LinearLayout>

Create Java Code File to Add,Update,Delete And Search Activity.

AddContactActivity.java



package com.example.bhaumik.sqldemo;

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 AddContactActivity extends AppCompatActivity {

    EditText name,email,mobile;
    Button add;

    DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(this);

        init();

        setListener();

    }

    private void setListener() {

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean result = databaseHelper.InsertData(name.getText().toString(),email.getText().toString(),mobile.getText().toString());

                if(result)
                {
                    Toast.makeText(AddContactActivity.this,"Contact Inserted Successfully...",Toast.LENGTH_SHORT).show();
                    name.setText("");
                    email.setText("");
                    mobile.setText("");
                }
                else
                {
                    Toast.makeText(AddContactActivity.this,"Failed Contact Inserted...",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {

        name = (EditText) findViewById(R.id.et_name);
        email = (EditText) findViewById(R.id.et_email);
        mobile  = (EditText) findViewById(R.id.et_mobile);
        add = (Button) findViewById(R.id.btn_add);

    }
}

UpdateContactActivity.java



package com.example.bhaumik.sqldemo;

import android.database.Cursor;
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 UpdateContactActivity extends AppCompatActivity {

    EditText mobile,name,email;
    Button search,update;

    DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(this);
        init();

        setListener();
    }

    private void setListener() {

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());

                while (cursor.moveToNext())
                {
                    name.setText(cursor.getString(1));
                    email.setText(cursor.getString(2));
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString());

                Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show();

                mobile.setText("");
                name.setText("");
                email.setText("");

            }
        });


    }

    private void init() {
        mobile = (EditText) findViewById(R.id.et_update_mobile);
        name = (EditText) findViewById(R.id.et_update_name);
        email = (EditText) findViewById(R.id.et_update_email);
        search = (Button) findViewById(R.id.btn_search_updates);
        update = (Button) findViewById(R.id.btn_update);
    }
}

DeleteContactActivity.java



package com.example.bhaumik.sqldemo;

import android.database.Cursor;
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 UpdateContactActivity extends AppCompatActivity {

    EditText mobile,name,email;
    Button search,update;

    DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(this);
        init();

        setListener();
    }

    private void setListener() {

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());

                while (cursor.moveToNext())
                {
                    name.setText(cursor.getString(1));
                    email.setText(cursor.getString(2));
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString());

                Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show();

                mobile.setText("");
                name.setText("");
                email.setText("");

            }
        });


    }

    private void init() {
        mobile = (EditText) findViewById(R.id.et_update_mobile);
        name = (EditText) findViewById(R.id.et_update_name);
        email = (EditText) findViewById(R.id.et_update_email);
        search = (Button) findViewById(R.id.btn_search_updates);
        update = (Button) findViewById(R.id.btn_update);
    }
}

SearchContactActivity.java



package com.example.bhaumik.sqldemo;

import android.database.Cursor;
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.TextView;

public class SearchContactActivity extends AppCompatActivity {

    EditText mobile;
    TextView name,email;
    Button search;

    DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(this);

        init();

        setListener();
    }

    private void setListener() {

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());

                while (cursor.moveToNext())
                {

                    name.setText(cursor.getString(1));
                    email.setText(cursor.getString(2));
                }
            }
        });
    }

    private void init() {

        mobile = (EditText) findViewById(R.id.et_search_mobile);
        name = (TextView) findViewById(R.id.tv_search_name);
        email = (TextView) findViewById(R.id.tv_search_email);
        search = (Button) findViewById(R.id.btn_search);
    }
}