For creating calculator add TextView and numaric Button into "activity_main.xml" file.
create a android apps with the name "calc".
create a android apps with the name "calc".
1.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:gravity="right"
android:text="@string/large_fix"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_toRightOf="@+id/Button01"
android:text="@string/fix2"
android:onClick="click"/>
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button02"
android:layout_alignBottom="@+id/Button02"
android:layout_toRightOf="@+id/Button02"
android:text="@string/fix3"
android:onClick="click"/>
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button03"
android:layout_alignBottom="@+id/Button03"
android:layout_toRightOf="@+id/Button03"
android:text="@string/plus"
android:onClick="clicked"/>
<Button
android:id="@+id/Button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button01"
android:layout_centerVertical="true"
android:text="@string/fix4"
android:onClick="click"/>
<Button
android:id="@+id/Button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/Button03"
android:text="@string/fix5"
android:onClick="click"/>
<Button
android:id="@+id/Button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/Button04"
android:text="@string/fix6"
android:onClick="click"/>
<Button
android:id="@+id/Button08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button07"
android:layout_alignBottom="@+id/Button07"
android:layout_alignLeft="@+id/Button04"
android:text="@string/minus"
android:onClick="clicked"/>
<Button
android:id="@+id/Button09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button05"
android:layout_toLeftOf="@+id/Button06"
android:text="@string/fix7"
android:onClick="click" />
<Button
android:id="@+id/Button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button09"
android:layout_alignBottom="@+id/Button09"
android:layout_toLeftOf="@+id/Button07"
android:text="@string/fix8"
android:onClick="click"/>
<Button
android:id="@+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button07"
android:layout_below="@+id/Button07"
android:text="@string/fix9"
android:onClick="click"/>
<Button
android:id="@+id/Button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button11"
android:layout_alignBottom="@+id/Button11"
android:layout_toRightOf="@+id/Button11"
android:text="@string/mul"
android:onClick="clicked" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button05"
android:layout_alignLeft="@+id/textView1"
android:text="@string/fix1"
android:onClick="click" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button10"
android:layout_toLeftOf="@+id/Button11"
android:text="@string/zero"
android:onClick="click"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button11"
android:layout_toRightOf="@+id/Button11"
android:text="@string/div"
android:onClick="clicked"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button10"
android:layout_toLeftOf="@+id/Button12"
android:text="@string/cfix"
android:onClick="clear" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button09"
android:layout_toLeftOf="@+id/Button10"
android:text="@string/dot"
android:onClick="click"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button3"
android:layout_marginBottom="33dp"
android:text="@string/large_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java
package com.example.calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display;
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display =(TextView) findViewById(R.id.textView1);
t2 =(TextView) findViewById(R.id.textView2);
display.setText("0");
}
static Boolean isempty=true;
public void click(View sender)
{
Button bt=(Button)sender;
if(isempty)
{
display.setText(bt.getText());
isempty=false;
}
else
{
display.append(bt.getText());
}
}
public void clear(View sender)
{
display.setText("0");
t2.setText("");
}
static float result=0;
static int opration=0;
public void clicked(View sender)
{
Button bt=(Button)sender;
switch(opration)
{
case 0:
result+=Float.parseFloat(display.getText().toString());
break;
case 1:
result-=Float.parseFloat(display.getText().toString());
break;
case 2:
result*=Float.parseFloat(display.getText().toString());
break;
case 3:
result/=Float.parseFloat(display.getText().toString());
break;
}
display.setText(Float.toString(result));
if(bt.getText().equals("+")) opration=0;
if(bt.getText().equals("-")) opration=1;
if(bt.getText().equals("*")) opration=2;
if(bt.getText().equals("/")) opration=3;
isempty=true;
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:gravity="right"
android:text="@string/large_fix"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_toRightOf="@+id/Button01"
android:text="@string/fix2"
android:onClick="click"/>
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button02"
android:layout_alignBottom="@+id/Button02"
android:layout_toRightOf="@+id/Button02"
android:text="@string/fix3"
android:onClick="click"/>
<Button
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button03"
android:layout_alignBottom="@+id/Button03"
android:layout_toRightOf="@+id/Button03"
android:text="@string/plus"
android:onClick="clicked"/>
<Button
android:id="@+id/Button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button01"
android:layout_centerVertical="true"
android:text="@string/fix4"
android:onClick="click"/>
<Button
android:id="@+id/Button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/Button03"
android:text="@string/fix5"
android:onClick="click"/>
<Button
android:id="@+id/Button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/Button04"
android:text="@string/fix6"
android:onClick="click"/>
<Button
android:id="@+id/Button08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button07"
android:layout_alignBottom="@+id/Button07"
android:layout_alignLeft="@+id/Button04"
android:text="@string/minus"
android:onClick="clicked"/>
<Button
android:id="@+id/Button09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button05"
android:layout_toLeftOf="@+id/Button06"
android:text="@string/fix7"
android:onClick="click" />
<Button
android:id="@+id/Button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button09"
android:layout_alignBottom="@+id/Button09"
android:layout_toLeftOf="@+id/Button07"
android:text="@string/fix8"
android:onClick="click"/>
<Button
android:id="@+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button07"
android:layout_below="@+id/Button07"
android:text="@string/fix9"
android:onClick="click"/>
<Button
android:id="@+id/Button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button11"
android:layout_alignBottom="@+id/Button11"
android:layout_toRightOf="@+id/Button11"
android:text="@string/mul"
android:onClick="clicked" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button05"
android:layout_alignLeft="@+id/textView1"
android:text="@string/fix1"
android:onClick="click" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button10"
android:layout_toLeftOf="@+id/Button11"
android:text="@string/zero"
android:onClick="click"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button11"
android:layout_toRightOf="@+id/Button11"
android:text="@string/div"
android:onClick="clicked"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button10"
android:layout_toLeftOf="@+id/Button12"
android:text="@string/cfix"
android:onClick="clear" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Button09"
android:layout_toLeftOf="@+id/Button10"
android:text="@string/dot"
android:onClick="click"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button3"
android:layout_marginBottom="33dp"
android:text="@string/large_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
MainActivity.java
package com.example.calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display;
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display =(TextView) findViewById(R.id.textView1);
t2 =(TextView) findViewById(R.id.textView2);
display.setText("0");
}
static Boolean isempty=true;
public void click(View sender)
{
Button bt=(Button)sender;
if(isempty)
{
display.setText(bt.getText());
isempty=false;
}
else
{
display.append(bt.getText());
}
}
public void clear(View sender)
{
display.setText("0");
t2.setText("");
}
static float result=0;
static int opration=0;
public void clicked(View sender)
{
Button bt=(Button)sender;
switch(opration)
{
case 0:
result+=Float.parseFloat(display.getText().toString());
break;
case 1:
result-=Float.parseFloat(display.getText().toString());
break;
case 2:
result*=Float.parseFloat(display.getText().toString());
break;
case 3:
result/=Float.parseFloat(display.getText().toString());
break;
}
display.setText(Float.toString(result));
if(bt.getText().equals("+")) opration=0;
if(bt.getText().equals("-")) opration=1;
if(bt.getText().equals("*")) opration=2;
if(bt.getText().equals("/")) opration=3;
isempty=true;
}
}