Android - CRUD Application
Android - CRUD Application
Operating System
ANDROID
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. <ListView
11. android:id="@+id/lv_list"
12. android:layout_width="match_parent"
13. android:layout_height="wrap_content"
14. android:layout_weight="1" />
15.
16. <RelativeLayout
17. android:layout_width="match_parent"
18. android:layout_height="wrap_content">
19. <Button
20. android:id="@+id/btn_add"
21. android:layout_width="wrap_content"
22. android:layout_height="wrap_content"
23. android:text="ADD"
24. android:ems="10"
25. android:layout_centerHorizontal="true" />
26.
27. </RelativeLayout>
28. </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. <Button
82. android:id="@+id/btn_regsave"
83. android:layout_height="wrap_content"
84. android:layout_width="wrap_content"
85. android:layout_marginRight="20dp"
86. android:layout_alignParentRight="true"
87. android:layout_below="@+id/tv_address"
88. android:text="SAVE"
89. android:ems="10"
90. android:layout_marginTop="30dp"/>
91.
92. <Button
93. android:id="@+id/btn_regback"
94. android:layout_height="wrap_content"
95. android:layout_width="wrap_content"
96. android:layout_marginLeft="20dp"
97. android:layout_alignParentLeft="true"
98. android:layout_below="@+id/tv_address"
99. android:text="BACK"
100. android:ems="10"
101. android:layout_marginTop="30dp"/>
102. </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.EmployeeDetai
l"
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. <Button
42. android:id="@+id/btn_uback"
43. android:layout_width="wrap_content"
44. android:layout_height="wrap_content"
45. android:layout_weight="1"
46. android:text="Back" />
47.
48. <Button
49. android:id="@+id/btn_delete"
50. android:layout_width="wrap_content"
51. android:layout_height="wrap_content"
52. android:layout_weight="1"
53. android:text="Delete" />
54.
55. <Button
56. android:id="@+id/btn_edit"
57. android:layout_width="wrap_content"
58. android:layout_height="wrap_content"
59. android:layout_weight="1"
60. android:text="Edit" />
61. </LinearLayout>
62. </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.UpdateEmploye
e"
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. <Button
65. android:id="@+id/btn_back"
66. android:layout_width="wrap_content"
67. android:layout_height="wrap_content"
68. android:layout_weight="1"
69. android:text="Back" />
70.
71. <Button
72. android:id="@+id/btn_update"
73. android:layout_width="wrap_content"
74. android:layout_height="wrap_content"
75. android:layout_weight="1"
76. android:text="Update" />
77. </LinearLayout>
78.
79. </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>
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. }
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!!!
Ad
1/2
00:30
Ngỡ Ngàng Trước Khung Cảnh Của Bình Liêu Quảng Ninh - Thiên Đường Vùng Biên
Giới Việt Trung
Download Code
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.