Skip to content

Instantly share code, notes, and snippets.

@Getmrahul
Last active March 7, 2016 06:57
Show Gist options
  • Save Getmrahul/e85b747b5b2c4e511313 to your computer and use it in GitHub Desktop.
Save Getmrahul/e85b747b5b2c4e511313 to your computer and use it in GitHub Desktop.
Android Simple Calculator
<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="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/Button04"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button03"
android:layout_below="@+id/Button03"
android:layout_marginTop="18dp"
android:onClick="onClickBtn"
android:text="*" />
<Button
android:id="@+id/Button06"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button04"
android:layout_alignBottom="@+id/Button04"
android:layout_toLeftOf="@+id/Button01"
android:onClick="onClickBtn"
android:text="4" />
<Button
android:id="@+id/Button12"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button11"
android:layout_below="@+id/Button11"
android:layout_marginTop="16dp"
android:onClick="onClickBtn"
android:text="+" />
<Button
android:id="@+id/Button01"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/button1"
android:onClick="onClickBtn"
android:text="8" />
<Button
android:id="@+id/Button02"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/Button01"
android:onClick="onClickBtn"
android:text="9" />
<Button
android:id="@+id/Button03"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button02"
android:layout_alignBottom="@+id/Button02"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/Button02"
android:onClick="onClickBtn"
android:text="/" />
<Button
android:id="@+id/Button05"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button04"
android:layout_alignBottom="@+id/Button04"
android:layout_toLeftOf="@+id/Button03"
android:onClick="onClickBtn"
android:text="6" />
<Button
android:id="@+id/Button14"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button12"
android:layout_alignBottom="@+id/Button12"
android:layout_toLeftOf="@+id/Button03"
android:onClick="onClickBtn"
android:text="=" />
<Button
android:id="@+id/Button15"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button14"
android:layout_alignBottom="@+id/Button14"
android:layout_toLeftOf="@+id/Button02"
android:onClick="onClickBtn"
android:text="0" />
<Button
android:id="@+id/Button13"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button15"
android:layout_alignBottom="@+id/Button15"
android:layout_toLeftOf="@+id/Button01"
android:onClick="onClickBtn"
android:text="&lt;-" />
<Button
android:id="@+id/Button07"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button05"
android:layout_alignBottom="@+id/Button05"
android:layout_toLeftOf="@+id/Button02"
android:onClick="onClickBtn"
android:text="5" />
<Button
android:id="@+id/Button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button04"
android:layout_below="@+id/Button04"
android:layout_marginTop="20dp"
android:onClick="onClickBtn"
android:text="-" />
<Button
android:id="@+id/Button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button12"
android:layout_alignLeft="@+id/Button14"
android:onClick="onClickBtn"
android:text="3" />
<Button
android:id="@+id/Button08"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button12"
android:layout_toLeftOf="@+id/Button02"
android:onClick="onClickBtn"
android:text="2" />
<Button
android:id="@+id/Button09"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Button12"
android:layout_toLeftOf="@+id/Button01"
android:onClick="onClickBtn"
android:text="1" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:onClick="onClickBtn"
android:text="7" />
</RelativeLayout>
package com.example.calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
String symbol = "";
float no1 = 0;
float no2 = 0;
Button btn;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
display.setText("Welcome");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setDisplay(float ans) {
no1 = 0;
no2 = no1;
symbol = "";
display.setText("Answer: "+ans);
}
public void onClickBtn(View v) {
if (no1 == 0 && no2 == 0 && symbol == ""){
display.setText("");
}
btn = (Button) findViewById(v.getId());
try {
int tmp = Integer.parseInt(btn.getText().toString());
if (symbol == "") {
if (no1 != 0) {
no1 = Integer.parseInt(Integer.toString((int) no1) + btn.getText().toString());
}
}
else {
if (no2 != 0) {
no2 = Integer.parseInt(Integer.toString((int) no2) + btn.getText().toString());
}
}
if (no1 == 0){
no1 = tmp;
}
else {
no2 = tmp;
}
display.setText(display.getText().toString() + " " + tmp);
}
catch (Exception e) {
String s = btn.getText().toString();
if (s.equals("=")) {
if (no1 == 0 && no2 == 0){
setDisplay(0);
}
else {
if (symbol.equals("+")) {
setDisplay(no1 + no2);
}
else if (symbol.equals("-")) {
setDisplay(no1 - no2);
}
else if (symbol.equals("*")) {
setDisplay(no1 * no2);
}
else if (symbol.equals("/")) {
setDisplay(no1/no2);
}
else {
no1 = 0;
no2 = no1;
symbol = "";
display.setText("Error");
}
}
}
else if(s.equals("<-")) {
if (no2 != 0)
no2 = 0;
else if (no1 != 0)
no1 = 0;
else
symbol = "";
}
else {
symbol = s;
display.setText(display.getText().toString() + " " + symbol);
}
}
}
public void onClick(View v) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment