"Android Simple Calculator Source Code", Edit main.xml file and activity file..!Below I addressed two file main.xml and P1Activity.java file for simple android calculator source code, You can easily copy and paste this code into your eclipse or android studio.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/ed3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/pluse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pluse" />
<Button
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minus" />
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mul" />
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/div" />
</TableRow>
</LinearLayout>
</LinearLayout>
P1Activity.java
package com.p1;
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;
public class P1Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText ed1,ed2,ed3;
Button pluse,min,mul,div;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1=(EditText)findViewById(R.id.ed1);
ed2=(EditText)findViewById(R.id.ed2);
ed3=(EditText)findViewById(R.id.ed3);
pluse=(Button)findViewById(R.id.pluse);
min=(Button)findViewById(R.id.min);
mul=(Button)findViewById(R.id.mul);
div=(Button)findViewById(R.id.div);
pluse.setOnClickListener(this);
min.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
String st1=ed1.getText().toString();
String st2=ed2.getText().toString();
int p=Integer.parseInt(st1);
int r=Integer.parseInt(st2);
if(arg0.getId()==R.id.pluse)
{
double a=p+r;
ed3.setText(""+a);
}
else if(arg0.getId()==R.id.min)
{
double a=p-r;
ed3.setText(""+a);
}
else if(arg0.getId()==R.id.mul)
{
double a=p*r;
ed3.setText(""+a);
}
else if(arg0.getId()==R.id.div)
{
double a=p/r;
ed3.setText(""+a);
}
else
{
}
}
}