--------------------------------------------------------------------------------------
Note:- First Create New Android Application(Project) And After Followed Following Step's
--------------------------------------------------------------------------------------
Step:- 1
XML: Go To Activity_main.xml OR main.xml File And Add Following xml Code ....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingTop="15dp">
<RelativeLayout android:layout_height="wrap_content"
android:id="@+id/relativeLayout1" android:layout_width="match_parent">
</RelativeLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout1">
<TextView android:text="Enter first values" android:id="@+id/txt1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold"></TextView>
<EditText android:text="" android:id="@+id/editText1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:singleLine="true"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout2">
<TextView android:text="Enter second value" android:id="@+id/txt2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/editText2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:singleLine="true"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout3"
android:padding="15dp">
<TextView android:text="Answer = " android:id="@+id/txt3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30dp"></TextView>
<TextView android:text="" android:id="@+id/txt4"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dp"></TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout4"
android:paddingTop="20dp" android:paddingLeft="55dp">
<Button android:text="+" android:id="@+id/btn1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:text="-" android:id="@+id/btn2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:text="*" android:id="@+id/btn3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="/" android:id="@+id/btn4"
android:textSize="40dp"></Button>
<Button android:text="%" android:id="@+id/btn5"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
</LinearLayout>
</LinearLayout>
--------------------------------------------------------------------------------------
Step:- 2
JAVA:- Go To src Folder --> Go To Java Package --> Go To .java File And Write Following java Code....
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class calculatertest extends Activity {
/** Called when the activity is first created. */
TextView tv1;
EditText ed1,ed2;
Button sum,sub,mul,div,mod;
int v1, v2 ,ans;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
tv1=(TextView)findViewById(R.id.txt4);
sum=(Button)findViewById(R.id.btn1);
sub=(Button)findViewById(R.id.btn2);
mul=(Button)findViewById(R.id.btn3);
div=(Button)findViewById(R.id.btn4);
mod=(Button)findViewById(R.id.btn5);
sum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1+v2;
tv1.setText(String.valueOf(ans).toString());
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1-v2;
tv1.setText(String.valueOf(ans).toString());
}
});
mul.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1*v2;
tv1.setText(String.valueOf(ans).toString());
}
});
div.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1/v2;
tv1.setText(String.valueOf(ans).toString());
}
});
mod.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1%v2;
tv1.setText(String.valueOf(ans).toString());
}
});
}
}
--------------------------------------------------------------------------------------
Step:- 3
Now Run Your Android Application(Project) and Check Your Project Output.......
OUTPUT:- Screen Short