Skip to main content

Android - Simple CRUD Application

Operating System


In this tutorial we will try to create a Simple CRUD Application using Android. Android is an open source so that developer find it easy to develop and expand new features. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So let's do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplecrudapplication">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity
  13. android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name=".Registration"
  24. android:configChanges="orientation"
  25. android:screenOrientation="portrait">
  26. <intent-filter>
  27. <action android:name="com.razormist.simplecrudapplication.Registration" />
  28.  
  29. <category android:name="android.intent.category.LAUNCHER" />
  30. </intent-filter>
  31. </activity>
  32. <activity android:name=".EmployeeDetail" />
  33. <activity android:name=".UpdateEmployee"></activity>
  34. </application>
  35. </manifest>

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplecrudapplication.MainActivity"
  8. android:orientation="vertical">
  9.  
  10. android:id="@+id/lv_list"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1" />
  14.  
  15. <RelativeLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content">
  18. android:id="@+id/btn_add"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="ADD"
  22. android:ems="10"
  23. android:layout_centerHorizontal="true" />
  24.  
  25. </RelativeLayout>
  26. </LinearLayout>

Now that we created the main interface we will now create another activities. To do that right click the app directory then choose new activity and select Empty Activity. Create 3 new activities called Registration, UpdateEmployee and EmployeeDetail, this also create a java file according to the activity name. Next write these blocks of code according to the activity xml file.
activity_registration.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplecrudapplication.Registration">
  8. <TextView
  9. android:id="@+id/tv_firstname"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Firstname"
  13. android:textSize="25sp"
  14. android:layout_marginLeft="20dp"
  15. android:layout_marginTop="64dp"
  16. android:layout_alignParentTop="true"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentStart="true"
  19. />
  20.  
  21. <EditText
  22. android:id="@+id/et_firstname"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:ems="10"
  26. android:inputType="text"
  27. android:layout_toRightOf="@+id/tv_firstname"
  28. android:layout_marginTop="55dp"
  29. android:layout_alignParentTop="true"
  30. android:layout_alignParentRight="true"
  31. />
  32.  
  33. <TextView
  34. android:id="@+id/tv_lastname"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="Lastname"
  38. android:textSize="25sp"
  39. android:layout_marginLeft="20dp"
  40. android:layout_marginTop="30dp"
  41. android:layout_alignParentLeft="true"
  42. android:layout_alignParentStart="true"
  43. android:layout_below="@id/tv_firstname"/>
  44.  
  45. <EditText
  46. android:id="@+id/et_lastname"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:ems="10"
  50. android:inputType="textPersonName"
  51. android:layout_toRightOf="@+id/tv_lastname"
  52. android:layout_marginTop="15dp"
  53. android:layout_alignParentRight="true"
  54. android:layout_below="@+id/et_firstname"
  55. />
  56.  
  57. <TextView
  58. android:id="@+id/tv_address"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:text="Address"
  62. android:textSize="25sp"
  63. android:layout_marginLeft="40dp"
  64. android:layout_marginTop="30dp"
  65. android:layout_alignParentLeft="true"
  66. android:layout_alignParentStart="true"
  67. android:layout_below="@id/tv_lastname"/>
  68.  
  69. <EditText
  70. android:id="@+id/et_address"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:ems="10"
  74. android:inputType="textPersonName"
  75. android:layout_toRightOf="@+id/tv_address"
  76. android:layout_marginTop="15dp"
  77. android:layout_alignParentRight="true"
  78. android:layout_below="@+id/et_lastname"
  79. />
  80.  
  81. android:id="@+id/btn_regsave"
  82. android:layout_height="wrap_content"
  83. android:layout_width="wrap_content"
  84. android:layout_marginRight="20dp"
  85. android:layout_alignParentRight="true"
  86. android:layout_below="@+id/tv_address"
  87. android:text="SAVE"
  88. android:ems="10"
  89. android:layout_marginTop="30dp"/>
  90.  
  91. android:id="@+id/btn_regback"
  92. android:layout_height="wrap_content"
  93. android:layout_width="wrap_content"
  94. android:layout_marginLeft="20dp"
  95. android:layout_alignParentLeft="true"
  96. android:layout_below="@+id/tv_address"
  97. android:text="BACK"
  98. android:ems="10"
  99. android:layout_marginTop="30dp"/>
  100. </RelativeLayout>

activity_employee_detail.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplecrudapplication.EmployeeDetail"
  8. android:orientation="vertical">
  9.  
  10. <TextView
  11. android:id="@+id/tv_name2"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Name"
  15. android:textStyle="bold"
  16. android:textSize="18sp"/>
  17.  
  18. <TextView
  19. android:id="@+id/tv_dname"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"/>
  22.  
  23. <TextView
  24. android:id="@+id/tv_address2"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="Address"
  28. android:textStyle="bold"
  29. android:textSize="18sp"/>
  30.  
  31. <TextView
  32. android:id="@+id/tv_daddress"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content" />
  35.  
  36. <LinearLayout
  37. android:layout_width="match_parent"
  38. android:layout_height="match_parent"
  39. android:orientation="horizontal">
  40.  
  41. android:id="@+id/btn_uback"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_weight="1"
  45. android:text="Back" />
  46.  
  47. android:id="@+id/btn_delete"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_weight="1"
  51. android:text="Delete" />
  52.  
  53. android:id="@+id/btn_edit"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_weight="1"
  57. android:text="Edit" />
  58. </LinearLayout>
  59. </LinearLayout>

activity_update_employee.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplecrudapplication.UpdateEmployee"
  8. android:orientation="vertical">
  9.  
  10.  
  11. <TextView
  12. android:id="@+id/textView"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="Firstname"
  16. android:textStyle="bold"
  17. android:textSize="18sp"/>
  18.  
  19. <EditText
  20. android:id="@+id/et_ufirstname"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:ems="10"
  24. android:ellipsize="end"
  25. android:inputType="text" />
  26.  
  27. <TextView
  28. android:id="@+id/textView2"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:text="Lastname"
  32. android:textStyle="bold"
  33. android:textSize="18sp"/>
  34.  
  35. <EditText
  36. android:id="@+id/et_ulastname"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:ems="10"
  40. android:ellipsize="end"
  41. android:inputType="text" />
  42.  
  43. <TextView
  44. android:id="@+id/textView3"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:text="Address"
  48. android:textStyle="bold"
  49. android:textSize="18sp"/>
  50.  
  51. <EditText
  52. android:id="@+id/et_uaddress"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:ems="10"
  56. android:ellipsize="end"
  57. android:inputType="text" />
  58.  
  59. <LinearLayout
  60. android:layout_width="match_parent"
  61. android:layout_height="match_parent"
  62. android:orientation="horizontal">
  63.  
  64. android:id="@+id/btn_back"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_weight="1"
  68. android:text="Back" />
  69.  
  70. android:id="@+id/btn_update"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:layout_weight="1"
  74. android:text="Update" />
  75. </LinearLayout>
  76.  
  77. </LinearLayout>

Lastly, create a layout for the list holder of all the data within a xml file. To do that create new XML file called employee_list.xml and write this block of codes inside of the file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_name"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:textSize="20sp"
  12. android:padding="5dp"/>
  13.  
  14. <TextView
  15. android:id="@+id/tv_address"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:padding="5dp"/>
  19. </LinearLayout>

Creating the List Adaptor

This will populate the data from the database to the ListView component. To do that create first create a new package called adapters then inside the new directory create a new java file called EmployeeListAdapter. Then write these block of codes insidee the java file.

  1. package com.razormist.simplecrudapplication.adapters;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5. import android.support.annotation.Nullable;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.TextView;
  11.  
  12. import com.razormist.simplecrudapplication.Employee;
  13. import com.razormist.simplecrudapplication.R;
  14.  
  15. import org.w3c.dom.Text;
  16.  
  17. import java.util.Comparator;
  18. import java.util.List;
  19.  
  20. public class EmployeeListAdapter extends ArrayAdapter<Employee> {
  21.  
  22. private Context context;
  23. private List<Employee>employees;
  24.  
  25. public EmployeeListAdapter(Context context, List<Employee> employees){
  26. super(context, R.layout.employee_list, employees);
  27. this.context = context;
  28. this.employees = employees;
  29. }
  30.  
  31. @NonNull
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  35. View view = layoutInflater.inflate(R.layout.employee_list, parent, false);
  36. TextView tv_name = (TextView)view.findViewById(R.id.tv_name);
  37. TextView tv_address = (TextView)view.findViewById(R.id.tv_address);
  38. tv_name.setText("Name: " + employees.get(position).getFirstname() + " " + employees.get(position).getLastname());
  39. tv_address.setText("Address: " + employees.get(position).getAddress());
  40.  
  41. return view;
  42. }
  43. }

Creating the Database

This is the code for the database, this will generate a database storage to be able to able to manage the data within the application. To do that create a new java file called Database, then write these block of codes inside the file.

  1. package com.razormist.simplecrudapplication;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.provider.ContactsContract;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. public class Database extends SQLiteOpenHelper{
  14. public static final String DATABASE_NAME = "employee.db";
  15. public static final String TABLE_NAME = "tbl_employee";
  16.  
  17. public Database(Context context) {
  18. super(context, DATABASE_NAME, null, 1);
  19.  
  20. }
  21.  
  22. @Override
  23. public void onCreate(SQLiteDatabase db) {
  24. db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, address TEXT)");
  25. }
  26.  
  27. @Override
  28. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  29. db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
  30. onCreate(db);
  31. }
  32.  
  33. public boolean InsertData(Employee employee){
  34. try {
  35. SQLiteDatabase db = getWritableDatabase();
  36. ContentValues contentValues = new ContentValues();
  37. contentValues.put("firstname", employee.getFirstname());
  38. contentValues.put("lastname", employee.getLastname());
  39. contentValues.put("address", employee.getAddress());
  40. long result = db.insert(TABLE_NAME, null, contentValues);
  41. db.close();
  42. if (result == -1) {
  43. return false;
  44. } else {
  45. return true;
  46. }
  47. }catch (Exception e){
  48. return false;
  49. }
  50. }
  51.  
  52. public List<Employee> DisplayAll(){
  53. try{
  54. List<Employee> employees = new ArrayList<Employee>();
  55. SQLiteDatabase sqLiteDatabase = getWritableDatabase();
  56. Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME, null);
  57. if(cursor.moveToFirst()){
  58. do{
  59. Employee employee = new Employee();
  60. employee.setId(cursor.getInt(0));
  61. employee.setFirstname(cursor.getString(1));
  62. employee.setLastname(cursor.getString(2));
  63. employee.setAddress(cursor.getString(3));
  64. employees.add(employee);
  65. }while (cursor.moveToNext());
  66. }
  67. sqLiteDatabase.close();
  68. return employees;
  69. }catch (Exception e){
  70. return null;
  71. }
  72.  
  73. }
  74.  
  75. public boolean Delete(int id){
  76. try{
  77. SQLiteDatabase db = getWritableDatabase();
  78. int row = db.delete(TABLE_NAME, "ID = ?", new String[]{String.valueOf(id)});
  79. db.close();
  80. return row > 0;
  81. }catch (Exception e){
  82. return false;
  83. }
  84. }
  85.  
  86. public boolean Update(Employee employee){
  87. try {
  88. SQLiteDatabase db = getWritableDatabase();
  89. ContentValues contentValues = new ContentValues();
  90. contentValues.put("firstname", employee.getFirstname());
  91. contentValues.put("lastname", employee.getLastname());
  92. contentValues.put("address", employee.getAddress());
  93. int row = db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{String.valueOf(employee.getId())});
  94. db.close();
  95. return row > 0;
  96. }catch (Exception e){
  97. return false;
  98. }
  99. }
  100.  
  101. }

Next, we will now create a Writing function and Read function to support the manipulation of data in the database. To do that create a java file called it as Employee and write these block of codes.
  1. package com.razormist.simplecrudapplication;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Employee implements Serializable{
  6. private int id;
  7. private String firstname;
  8. private String lastname;
  9. private String address;
  10.  
  11. public int getId() {
  12. return id;
  13. }
  14.  
  15. public void setId(int id) {
  16. this.id = id;
  17. }
  18.  
  19. public String getFirstname() {
  20. return firstname;
  21. }
  22.  
  23. public void setFirstname(String firstname) {
  24. this.firstname = firstname;
  25. }
  26.  
  27. public String getLastname() {
  28. return lastname;
  29. }
  30.  
  31. public void setLastname(String lastname) {
  32. this.lastname = lastname;
  33. }
  34.  
  35. public String getAddress() {
  36. return address;
  37. }
  38.  
  39. public void setAddress(String address) {
  40. this.address = address;
  41. }
  42.  
  43. public Employee(int id, String firstname, String lastname, String address) {
  44. this.id = id;
  45. this.firstname = firstname;
  46. this.lastname = lastname;
  47. this.address = address;
  48. }
  49.  
  50. public Employee() {
  51. }
  52. }

Creating the Main Function

This code contains a several code that manages the data in the database. This is the main function that contain the most important part of the application the Create Read, Update, and Delete. To do that write these code inside the java file according to the given file name.

MainActivity

  1. package com.razormist.simplecrudapplication;
  2.  
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.provider.ContactsContract;
  6. import android.support.v4.media.RatingCompat;
  7. import android.support.v4.widget.SimpleCursorAdapter;
  8. import android.support.v7.app.AlertDialog;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.AdapterView;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.ListView;
  17. import android.widget.Toast;
  18.  
  19. import com.razormist.simplecrudapplication.adapters.EmployeeListAdapter;
  20.  
  21. import java.util.Comparator;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24. Database database;
  25. EditText et_firstname, et_lastname, et_address;
  26. Button btn_add, btn_view;
  27. ListView lv_list;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. database = new Database(this);
  34.  
  35. btn_add = (Button)findViewById(R.id.btn_add);
  36. lv_list = (ListView)findViewById(R.id.lv_list);
  37.  
  38. EmployeeListAdapter employeeListAdapter = new EmployeeListAdapter(this, database.DisplayAll());
  39. employeeListAdapter.sort(new Comparator<Employee>() {
  40. @Override
  41. public int compare(Employee lhs, Employee rhs) {
  42. return lhs.getLastname().compareTo(rhs.getLastname());
  43. }
  44. });
  45. lv_list.setAdapter(employeeListAdapter);
  46. lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  47. @Override
  48. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  49. Employee employee = database.DisplayAll().get(position);
  50. Intent intent = new Intent(MainActivity.this, EmployeeDetail.class);
  51. intent.putExtra("employee", employee);
  52. startActivity(intent);
  53. }
  54. });
  55. btn_add.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. Intent intent = new Intent(MainActivity.this, Registration.class);
  59. startActivity(intent);
  60. }
  61. });
  62.  
  63.  
  64. }
  65. }

Registration

  1. package com.razormist.simplecrudapplication;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. public class Registration extends AppCompatActivity {
  12. Database database;
  13. EditText et_regfirstname, et_reglastname, et_regaddress;
  14. Button btn_regsave, btn_regback;
  15.  
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_registration);
  21.  
  22. database = new Database(this);
  23.  
  24. et_regfirstname = (EditText)findViewById(R.id.et_firstname);
  25. et_reglastname = (EditText)findViewById(R.id.et_lastname);
  26. et_regaddress = (EditText)findViewById(R.id.et_address);
  27. btn_regsave = (Button)findViewById(R.id.btn_regsave);
  28. btn_regback = (Button)findViewById(R.id.btn_regback);
  29.  
  30. btn_regsave.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. String firstname = et_regfirstname.getText().toString();
  34. String lastname = et_reglastname.getText().toString();
  35. String address = et_regaddress.getText().toString();
  36.  
  37. if(!firstname.equals("") || !lastname.equals("") || !address.equals("")){
  38.  
  39. Employee employee = new Employee();
  40. employee.setFirstname(firstname);
  41. employee.setLastname(lastname);
  42. employee.setAddress(address);
  43.  
  44. if (database.InsertData(employee)) {
  45. Toast.makeText(Registration.this, "Successfully Inserted Data", Toast.LENGTH_SHORT).show();
  46. et_regfirstname.setText("");
  47. et_reglastname.setText("");
  48. et_regaddress.setText("");
  49. }
  50. }else{
  51. Toast.makeText(Registration.this, "Please complete the required field!", Toast.LENGTH_SHORT).show();
  52. }
  53. }
  54. });
  55.  
  56. btn_regback.setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. Intent intent = new Intent(Registration.this, MainActivity.class);
  60. startActivity(intent);
  61. }
  62. });
  63.  
  64. }
  65. }

EmployeeDetail

  1. package com.razormist.simplecrudapplication;
  2.  
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.preference.DialogPreference;
  6. import android.support.v7.app.AlertDialog;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class EmployeeDetail extends AppCompatActivity {
  14. private TextView tv_dname, tv_daddress;
  15. private Button btn_back, btn_edit, btn_delete;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_employee_detail);
  21.  
  22. final Employee employee = (Employee) getIntent().getSerializableExtra("employee");
  23. tv_dname = (TextView)findViewById(R.id.tv_dname);
  24. tv_daddress = (TextView)findViewById(R.id.tv_daddress);
  25. this.btn_back = (Button)findViewById(R.id.btn_uback);
  26. btn_edit = (Button)findViewById(R.id.btn_edit);
  27. btn_delete = (Button)findViewById(R.id.btn_delete);
  28. tv_dname.setText(employee.getFirstname() + " " + employee.getLastname());
  29. tv_daddress.setText(employee.getAddress());
  30.  
  31. this.btn_back.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. Intent intent = new Intent(EmployeeDetail.this, MainActivity.class);
  35. startActivity(intent);
  36. }
  37. });
  38.  
  39. btn_edit.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. Intent intent = new Intent(EmployeeDetail.this, UpdateEmployee.class);
  43. intent.putExtra("employee", employee);
  44. startActivity(intent);
  45. }
  46. });
  47.  
  48. btn_delete.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(final View v) {
  51. final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
  52. builder.setTitle("Confirmation");
  53. builder.setMessage("Are you sure you want to delete this record?");
  54. builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  55. @Override
  56. public void onClick(DialogInterface dialog, int which) {
  57. Database database = new Database(getBaseContext());
  58. if(database.Delete(employee.getId())){
  59. Intent intent = new Intent(EmployeeDetail.this, MainActivity.class);
  60. startActivity(intent);
  61. }else{
  62. AlertDialog.Builder builder1 = new AlertDialog.Builder(getBaseContext());
  63. builder1.setMessage("Fail");
  64. builder1.setCancelable(false);
  65. builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  66. @Override
  67. public void onClick(DialogInterface dialog, int which) {
  68. dialog.cancel();
  69. }
  70. });
  71. builder1.create().show();
  72. }
  73. }
  74. });
  75. builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
  76. @Override
  77. public void onClick(DialogInterface dialog, int which) {
  78. dialog.cancel();
  79. }
  80. });
  81. builder.create().show();
  82. }
  83. });
  84. }
  85. }

UpdateEmployee

  1. package com.razormist.simplecrudapplication;
  2.  
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. public class UpdateEmployee extends AppCompatActivity {
  14. private EditText et_ufirstname, et_ulastname, et_uaddress;
  15. private Button btn_back, btn_update;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_update_employee);
  21. final Database db = new Database(getBaseContext());
  22. final Employee employee = (Employee)getIntent().getSerializableExtra("employee");
  23. et_ufirstname = (EditText)findViewById(R.id.et_ufirstname);
  24. et_ulastname = (EditText)findViewById(R.id.et_ulastname);
  25. et_uaddress = (EditText)findViewById(R.id.et_uaddress);
  26. this.btn_back = (Button)findViewById(R.id.btn_back);
  27. btn_update = (Button)findViewById(R.id.btn_update);
  28.  
  29. et_ufirstname.setText(employee.getFirstname());
  30. et_ulastname.setText(employee.getLastname());
  31. et_uaddress.setText(employee.getAddress());
  32. et_ufirstname.setSelection(et_ufirstname.getText().length());
  33. et_ulastname.setSelection(et_ulastname.getText().length());
  34. et_uaddress.setSelection(et_uaddress.getText().length());
  35.  
  36. this.btn_back.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. Intent intent = new Intent(UpdateEmployee.this, EmployeeDetail.class);
  40. intent.putExtra("employee", employee);
  41. startActivity(intent);
  42. }
  43. });
  44.  
  45. btn_update.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. String firstname = et_ufirstname.getText().toString();
  49. String lastname = et_ulastname.getText().toString();
  50. String address = et_uaddress.getText().toString();
  51.  
  52. employee.setFirstname(firstname);
  53. employee.setLastname(lastname);
  54. employee.setAddress(address);
  55.  
  56.  
  57. if(db.Update(employee)){
  58. //Toast.makeText(UpdateEmployee.this, "SUCCESS!", Toast.LENGTH_SHORT).show();
  59. AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
  60. builder.setTitle("System");
  61. builder.setMessage("SUCCESSFULLY UPDATED");
  62. builder.setCancelable(false);
  63. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  64. @Override
  65. public void onClick(DialogInterface dialog, int which) {
  66. Intent intent = new Intent(UpdateEmployee.this, MainActivity.class);
  67. startActivity(intent);
  68. }
  69. });
  70. builder.create().show();
  71. }else{
  72. Toast.makeText(UpdateEmployee.this, "FAILED!", Toast.LENGTH_SHORT).show();
  73. }
  74. }
  75. });
  76. }
  77. }

Try to run the app and see if it worked.

There you have it we have created a Simple CRUD Application using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byhuriya (not verified)on Tue, 04/03/2018 - 21:29

Can anyone help me in developong expense manager app ..plzzzzzz

Submitted byrazormiston Thu, 04/12/2018 - 14:20

In reply to by huriya (not verified)

I hope that this link can help you https://www.sourcecodester.com/android/11491/expense-manager.html

Submitted bynewbie9999on Sat, 06/08/2019 - 17:49

Hello razormist,
First of all, a really great example, it helped me a lot.
But I find there is one thing not working properly for me at least.
As your code sorts the list based on the "Last Name", when I click on a row, the selection seems being done by the "Id", rather than by the "position" it is shown on the screen.
For example, if I first save a record with a "Last Name" = Smith and then a second one with "Last Name" = Johnson, the list will show Johnson as first and the Smith, because of the sorting by Last Name.
But then, if I select on Johnson to Edit/Delete, what I see the app is selecting is the one with Smith.
Any solution to this?
Thank you.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.