Introduction:
This article is a continuation of my article titled "Insert, Delete and View functionalities in Database through Android Studio". In the previous article, you saw how to implement the addition, deletion and view functionalities in a database. I used a bank database for this purpose. Admin functionalities were already implemented. Today, we will make customer functionalities, in other words withdraw cash, deposit cash and view account details.
For withdrawing and depositing, basically the database needs to be updated.
Step 1:
Add the following code to the previously edited "string.xl":
<string name="custom">Customer</string>
<string name="custtxt">Welcome Customer.....</string>
Note that the other files in "values", in other words "color.xml", "dimens.xml" and "styles.xml" remain the same.
Step 2:
For beautification, all my buttons will use the following drawable resource as their background. Right-click on "Drawable" -> "New" -> "Drawable resource file". Name this file as "admin_design" and add the following to it:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" >
<shape android:shape="rectangle">
<gradient
android:startColor="#454545"
android:endColor="#454545"
android:angle="-90"
android:type="linear"
/>
<corners android:radius="5dp" />
</shape>
</item>
<!-- pressed -->
<item android:state_pressed="true" android:state_enabled="true" >
<shape android:shape="rectangle">
<gradient
android:startColor="#64334C"
android:endColor="#300019"
android:angle="-90"
android:type="linear"
/>
<corners android:radius="5dp" />
</shape>
</item>
<!-- focused -->
<item android:state_focused="true">
<shape android:shape="rectangle">
<gradient
android:startColor="#C76699"
android:endColor="#d9c292"
android:angle="-90"
android:type="linear"
/>
<corners android:radius="5dp" />
<stroke android:width="2dp" android:color="#dddddd"/>
</shape>
</item>
<!-- default -->
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#C76699"
android:endColor="#d9c292"
android:angle="-90"
android:type="linear"
/>
<corners android:radius="5dp" />
</shape>
</item>
</selector>
Step 3:
Now let us make the customer page layout. Right-click on "Layout" -> "New" -> "Layout resource file". Name this file as "customer_layout". Add the following code to this XML file (inside the LinearLayout element):
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/custtxt"
android:layout_marginLeft="40dp"
android:textColor="@color/txt"
android:layout_marginTop="20dp"
android:textSize="@dimen/wel_admin"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:text="Back To Main Menu"
android:background="@drawable/admin_design"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/backcust"
/>
<Button
android:id="@+id/withdaw"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="40dp"
android:background="@drawable/admin_design"
android:text="Withdraw"
android:paddingLeft="10dp"
android:paddingRight="10dp"
/>
<Button
android:id="@+id/deposit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="40dp"
android:background="@drawable/admin_design"
android:text="Deposit"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<Button
android:id="@+id/cview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="40dp"
android:background="@drawable/admin_design"
android:text="View"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
The layout looks like:
Step 4:
To create a layout for withdrawing the cash: right-click on "Layout" -> "New" -> "Layout resource file". Name this file "withdraw_layout" and add the following to it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/withdarw">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Withdraw the cash....."
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="40dp"
android:textSize="30dp"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/t1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Your Id:"
android:textSize="@dimen/form_ele"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/t1"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:id="@+id/wid"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/t2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Amount:"
android:textSize="@dimen/form_ele"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/t2"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:id="@+id/wamt"/>
</RelativeLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Withdraw"
android:layout_marginTop="80dp"
android:layout_marginLeft="110dp"
android:background="@drawable/admin_design"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:id="@+id/bwithdraw"/>
</LinearLayout>
Note that in this layout I have used an image as the background. The method is the same. Copy the image you want to be the background to the clipboard and paste it in the "drawable" folders.
The layout looks like:
Step 5:
Similarly, create a layout file for depositing cash and name it "deposit_layout". Add the following code to it (inside the LinearLayout element):
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Deposit the cash....."
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="40dp"
android:textSize="30dp"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/t3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Your Id:"
android:textSize="@dimen/form_ele"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/t3"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:id="@+id/did"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/t4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Amount:"
android:textSize="@dimen/form_ele"
android:layout_marginLeft="10dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/t4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/damt"/>
</RelativeLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Deposit"
android:layout_marginTop="80dp"
android:layout_marginLeft="110dp"
android:background="@drawable/admin_design"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:id="@+id/bdeposit"/>
The layout looks like:
Step 6:
Create a layout file for depositing cash and name it as "cview_layout". Add the following code to it (inside the LinearLayout element):
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View account...."
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:textSize="@dimen/wel_admin"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/d1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Acc id: "
android:layout_marginLeft="20dp"
android:layout_marginTop="60dp"
android:textSize="@dimen/form_ele"/>
<EditText
android:id="@+id/cidv"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="60dp"
android:layout_toRightOf="@id/t1"
android:layout_marginLeft="110dp"/>
</RelativeLayout>
<Button
android:id="@+id/bview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="130dp"
android:text="View"
android:background="@drawable/admin_design"/>
The layout looks like:
Step 7:
I am creating yet another layout for viewing the database. The previous layout was asking for the user id. This layout will display the details of the customer depending upon the id provided by the user.
In the same way, create another layout file and name it "cview_layout2". Add the following code to it (inside the LinearLayout element):
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/lav"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="30dp"/>
The layout looks like:
Now let us start with the Java part.
Step 8:
Go to the "MainActivity" Java file (already modified for the Admin functionalities) and add the following code to it:
Button b2;
b2=(Button)findViewById(R.id.customer);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(context,Customer.class);
startActivity(i);
}
});
Here we have just added the click event for the customer button.
Step 9:
In the same package, right-click then select "New" -> "Java class". Name this file as "Customer" and add the following code to it:
package com.example.bankdb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Customer extends Activity {
Button w;
Button d;
Button v;
Button backc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_layout);
final Context context=this;
w=(Button)findViewById(R.id.withdaw);
d=(Button)findViewById(R.id.deposit);
v=(Button)findViewById(R.id.cview);
backc=(Button)findViewById(R.id.backcust);
w.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(context, Withdraw.class);
startActivity(i);
}
});
d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(context, Deposit.class);
startActivity(i);
}
});
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(context, Cview.class);
startActivity(i);
}
});
backc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Note the use of "finish()". This function finishes the current activity and load the activity that had called this activity.
Step 10:
Create a new Java class in the same way. Name it as "Withdraw" and add the following code to it:
package com.example.bankdb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Withdraw extends Activity {
Button b;
EditText wid;
EditText wamt;
SQLiteDatabase db;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.withdraw_layout);
final Context context=this;
wid=(EditText)findViewById(R.id.wid);
wamt=(EditText)findViewById(R.id.wamt);
b=(Button)findViewById(R.id.bwithdraw);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try
{
String t=wid.getText().toString();
int t4=Integer.parseInt(t);
int t5=Integer.parseInt(wamt.getText().toString());
db=openOrCreateDatabase("Banking1",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c= db.rawQuery("SELECT * FROM bank where id="+t,null);
c.moveToFirst();
int flag=0;
s="Withdraw";
Comman.fun(s);
while(! c.isAfterLast())
{
int t2=Integer.parseInt(c.getString(0));
int t3=Integer.parseInt(c.getString(2));
if(t4==t2)
{
flag=1;
if((t3-t5)>0)
{
int temp=t3-t5;
db.execSQL("UPDATE bank SET bal="+temp+" WHERE id="+t4);
Comman.fun("Withdraw","Problem updating..");
Toast.makeText(Withdraw.this, "Withdrawn......", 2000).show();
}
else
{
Toast.makeText(Withdraw.this, "Not enough cash......", 2000).show();
}
}
c.moveToNext();
}
if(flag==0)
{
Toast.makeText(Withdraw.this, "Invalid Id.............", 2000).show();
}
wid.setText("");
wamt.setText("");
//Intent i=new Intent(context,Customer.class);
//startActivity(i);
finish();
}
catch(Exception e)
{
}
}
});
}
}
Step 11:
Create another activity and name it "Deposit". Add the following to it:
package com.example.bankdb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Deposit extends Activity {
Button b;
EditText did;
EditText damt;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deposit_layout);
final Context context=this;
did=(EditText)findViewById(R.id.did);
damt=(EditText)findViewById(R.id.damt);
b=(Button)findViewById(R.id.bdeposit);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try
{
int flag=0;
String t=did.getText().toString();
int t4=Integer.parseInt(t);
int t5=Integer.parseInt(damt.getText().toString());
db=openOrCreateDatabase("Banking1",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c= db.rawQuery("SELECT * FROM bank where id="+t,null);
c.moveToFirst();
while(! c.isAfterLast())
{
int t2=Integer.parseInt(c.getString(0));
int t3=Integer.parseInt(c.getString(2));
if(t4==t2)
{
int temp=t5+t3;
db.execSQL("UPDATE bank SET bal="+temp+" WHERE id="+t4);
Toast.makeText(Deposit.this, "Deposited......", 2000).show();
flag=1;
}
c.moveToNext();
}
if(flag==0)
{
Toast.makeText(Deposit.this, "Invalid Id.............", 2000).show();
}
did.setText("");
damt.setText("");
//Intent i=new Intent(context,Customer.class);
//startActivity(i);
}
catch(Exception e)
{
e.printStackTrace();
}
finish();
}
});
}
}
Step 12:
Create an activity for viewing the database. Name this activity as "Cview". Add the following to it:
package com.example.bankdb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.database.sqlite.SQLiteDatabase;
import android.widget.TextView;
import android.widget.Toast;
public class Cview extends Activity {
EditText cidv;
Button bview;
Button bac;
Button backvc;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cview_layout);
final Context context=this;
bview=(Button)findViewById(R.id.bview);
cidv=(EditText)findViewById(R.id.cidv);
bac=(Button)findViewById(R.id.backcview);
backvc=(Button)findViewById(R.id.backvc);
bview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String t=cidv.getText().toString();
db=openOrCreateDatabase("Banking1",SQLiteDatabase.CREATE_IF_NECESSARY,null);
Cursor c= db.rawQuery("SELECT * FROM bank where id="+t,null);
c.moveToFirst();
int flag=0;
while(! c.isAfterLast())
{
String t2=c.getString(0);
String t3=c.getString(1);
String t4=c.getString(2);
flag=1;
setContentView(R.layout.cview_layout2);
TextView e=(TextView)findViewById(R.id.lav);
e.setText("Id: "+t2+" Type: "+t3+" Bal: "+t4);
c.moveToNext();
}
if(flag==0)
{
cidv.setText("");
Toast.makeText(Cview.this, "Invalid ID.....", 2000).show();
Intent i=new Intent(context,Customer.class);
startActivity(i);
//finish();
}
}
});
bac.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//finish();
Intent i=new Intent(context,Customer.class);
startActivity(i);
}
});
backvc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
Intent i=new Intent(context,Customer.class);
startActivity(i);
}
});
}
}
Step 13:
In the end, don't forget to add the names of the new activities to the manifest file, in other words "AndroidMannifest.xml". Append the following code to this file (already modified for Admin):
<activity android:name=".Customer"
android:label="Customer"/>
<activity android:name=".Cview"
android:label="View"/>
<activity android:name=".Deposit"
android:label="Deposit"/>
<activity android:name=".Withdraw"
android:label="Withdraw"/>
The output snapshots:
Clicking on Customer will give you:
Clicking on Withdraw will give you:
When you will click on Withdraw it will return to the Customer menu. Selecting Deposit this time will give you:
When you click on Deposit it will return to the Customer menu. Selecting View this time will give you:
Clicking on View will give you:
This ends my article. I hope that database connectivity is fully clear to you now.
Thank you.... Enjoy Coding :)