0% found this document useful (0 votes)
36 views5 pages

Android Calculator Layout Code

The document describes an XML code and Java code for building a basic calculator app layout and functionality using Android Studio. The XML code defines the calculator button layout in a table. The Java code initializes the buttons and handles click events to perform calculations and display results.

Uploaded by

Nisha Parchande
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Android Calculator Layout Code

The document describes an XML code and Java code for building a basic calculator app layout and functionality using Android Studio. The XML code defines the calculator button layout in a table. The Java code initializes the buttons and handles click events to perform calculations and display results.

Uploaded by

Nisha Parchande
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Calculator

XML CODE:

<TableLayout
xmlns:android="[Link] <Button
apk/res/android" android:id="@+id/btn_two"
android:id="@+id/tl_main" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="20dp"
android:layout_gravity="center_horizontal" android:paddingRight="20dp"
> android:text="2" />

<TableRow <Button
android:id="@+id/tr_first" android:id="@+id/btn_three"
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="5dp" android:paddingLeft="20dp"
android:paddingLeft="5dp" android:paddingRight="20dp"
android:paddingRight="5dp" > android:text="3" />

<EditText <Button
android:id="@+id/et_result" android:id="@+id/btn_plus"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ems="10" android:paddingLeft="20dp"
android:layout_span="4" android:paddingRight="20dp"
android:inputType="number" /> android:text="+" />

</TableRow> </TableRow>

<TableRow <TableRow
android:id="@+id/tr_second" android:id="@+id/tr_third"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="5dp" android:paddingLeft="5dp"
android:paddingRight="5dp" > android:paddingRight="5dp" >

<Button <Button
android:id="@+id/btn_one" android:id="@+id/btn_four"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="20dp" android:paddingLeft="20dp"
android:paddingRight="20dp" android:paddingRight="20dp"
android:text="1" /> android:text="4" />
android:paddingRight="20dp"
<Button android:text="8" />
android:id="@+id/btn_five"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/btn_nine"
android:paddingLeft="20dp" android:layout_width="wrap_content"
android:paddingRight="20dp" android:layout_height="wrap_content"
android:text="5" /> android:paddingLeft="20dp"
android:paddingRight="20dp"
<Button android:text="9" />
android:id="@+id/btn_six"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/btn_times"
android:paddingLeft="20dp" android:layout_width="wrap_content"
android:paddingRight="20dp" android:layout_height="wrap_content"
android:text="6" /> android:paddingLeft="20dp"
android:paddingRight="20dp"
<Button android:text="*" />
android:id="@+id/btn_minus"
android:layout_width="wrap_content" </TableRow>
android:layout_height="wrap_content"
android:paddingLeft="20dp" <TableRow
android:paddingRight="20dp" android:id="@+id/tr_fifth"
android:text="-" /> android:layout_width="fill_parent"
android:layout_height="wrap_content"
</TableRow> android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="@+id/tr_fourth" <Button
android:layout_width="fill_parent" android:id="@+id/btn_zero"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="5dp" android:layout_height="wrap_content"
android:paddingRight="5dp" > android:paddingLeft="20dp"
android:paddingRight="20dp"
<Button android:text="0" />
android:id="@+id/btn_seven"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/btn_clear"
android:paddingLeft="20dp" android:layout_width="wrap_content"
android:paddingRight="20dp" android:layout_height="wrap_content"
android:text="7" /> android:paddingLeft="20dp"
android:paddingRight="20dp"
<Button android:text="C" />
android:id="@+id/btn_eight"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/btn_equals"
android:paddingLeft="20dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" setContentView([Link].activity_main);
android:paddingLeft="20dp" initializeVars();
android:paddingRight="20dp" }
android:text="=" />
public void initializeVars(){
<Button result =
android:id="@+id/btn_divide" (EditText)findViewById([Link].et_result);
android:layout_width="wrap_content" one =
android:layout_height="wrap_content" (Button)findViewById([Link].btn_one);
android:paddingLeft="20dp" two =
android:paddingRight="20dp" (Button)findViewById([Link].btn_two);
android:text="/" /> three =
<!-- android:layout_column="3"--> (Button)findViewById([Link].btn_three);
</TableRow> four =
</TableLayout> (Button)findViewById([Link].btn_four);
five =
(Button)findViewById([Link].btn_five);
JAVA CODE: six = (Button)findViewById([Link].btn_six);
seven =
package [Link];
(Button)findViewById([Link].btn_seven);
eight =
import [Link];
(Button)findViewById([Link].btn_eight);
import [Link];
nine =
import [Link];
(Button)findViewById([Link].btn_nine);
import [Link];
zero =
import
(Button)findViewById([Link].btn_zero);
[Link];
plus =
import [Link];
(Button)findViewById([Link].btn_plus);
import [Link];
minus =
(Button)findViewById([Link].btn_minus);
public class MainActivity extends Activity
times =
implements OnClickListener{
(Button)findViewById([Link].btn_times);
divide =
String res, res_plus, res_minus,
(Button)findViewById([Link].btn_divide);
res_times, res_divide;
equals =
Integer wswitch;
(Button)findViewById([Link].btn_equals);
Double answer = 0.0;
clear =
EditText result;
(Button)findViewById([Link].btn_clear);
Button one, two, three, four, five, six,
seven, eight, nine, zero, plus, minus,
[Link](this);
times, divide, clear, equals;
[Link](this);
[Link](this);
@Override
[Link](this);
protected void onCreate(Bundle
[Link](this);
savedInstanceState) {
[Link](this);
[Link](savedInstanceState);
[Link](this);
[Link](this); case [Link].btn_six:
[Link](this); res = [Link]().toString()+"6";
[Link](this); [Link](res);
[Link](this); break;
[Link](this); case [Link].btn_seven:
[Link](this); res = [Link]().toString()+"7";
[Link](this); [Link](res);
[Link](this); break;
[Link](this); case [Link].btn_eight:
res = [Link]().toString()+"8";
} [Link](res);
break;
@Override case [Link].btn_nine:
public boolean res = [Link]().toString()+"9";
onCreateOptionsMenu(Menu menu) { [Link](res);
// Inflate the menu; this adds items to break;
the action bar if it is present. case [Link].btn_zero:
getMenuInflater().inflate([Link], res = [Link]().toString()+"0";
menu); [Link](res);
return true; break;
} case [Link].btn_plus:

@Override res_plus = [Link]().toString();


public void onClick(View arg0) { [Link]("");
// TODO Auto-generated method stub wswitch = 1;
switch ([Link]()){ break;
case [Link].btn_one: case [Link].btn_minus:
res = [Link]().toString()+"1"; res_minus = [Link]().toString();
[Link](res); [Link]("");
break; wswitch = 2;
case [Link].btn_two: break;
res = [Link]().toString()+"2"; case [Link].btn_times:
[Link](res); res_times = [Link]().toString();
break; [Link]("");
case [Link].btn_three: wswitch = 3;
res = [Link]().toString()+"3"; break;
[Link](res); case [Link].btn_divide:
break; res_divide = [Link]().toString();
case [Link].btn_four: [Link]("");
res = [Link]().toString()+"4"; wswitch = 4;
[Link](res); break;
break; case [Link].btn_equals:
case [Link].btn_five: switch(wswitch){
res = [Link]().toString()+"5"; case 1:
[Link](res); if(res_plus.matches("") ||
break; [Link]().toString().matches("")){
[Link](""); }
}else{ break;
answer = case [Link].btn_clear:
[Link](res_plus)+[Link] [Link]("");
eDouble([Link]().toString()); break;
res = [Link](answer); }
[Link](res); }
}
break; }
case 2:
if(res_minus.matches("") ||
[Link]().toString().matches("")){
[Link]("");
}else{
answer =
[Link](res_minus)-
[Link]([Link]().toStr
ing());
res = [Link](answer);
[Link](res);
}
break;
case 3:
if(res_times.matches("") ||
[Link]().toString().matches("")){
[Link]("");
}else{
answer =
[Link](res_times)*[Link]
rseDouble([Link]().toString());
res = [Link](answer);
[Link](res);
}
break;
case 4:
if(res_divide.matches("") ||
[Link]().toString().matches("")){
[Link]("");
}else{
answer =
[Link](res_divide)/Double.p
arseDouble([Link]().toString());
res = [Link](answer);
[Link](res);
}
break;

You might also like