Skip to content

Instantly share code, notes, and snippets.

@DanyshMushtaq
Last active November 9, 2015 10:14
Show Gist options
  • Save DanyshMushtaq/1e550ccf00d93d60d108 to your computer and use it in GitHub Desktop.
Save DanyshMushtaq/1e550ccf00d93d60d108 to your computer and use it in GitHub Desktop.
SIMPLE CALCULATOR ANDROID ------- RELEASED IN PUBLIC INTEREST
package com.example.administrator.mysampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IntegerRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
/*public View.OnClickListener myClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};*/
Float num1;
Float num2;
int flag=0;
char op;
EditText res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
res=(EditText) findViewById(R.id.res);
//Button btn1 = (Button) findViewById(R.id.no1);
//btn1.setOnClickListener(this);
//btn1.setOnClickListener(myClickListener);
}
public void buttonClick2(View view){
Intent intent=new Intent(this,MyRelativeActivity.class);
//startActivity(intent);
startActivityForResult(intent, 123);
//finish();
}
public void buttonClick(View view){
switch(view.getId())
{
case R.id.no0: res.append("0");break;
case R.id.no1: res.append("1");break;
case R.id.no2: res.append("2");break;
case R.id.no3: res.append("3");break;
case R.id.no4: res.append("4");break;
case R.id.no5: res.append("5");break;
case R.id.no6: res.append("6");break;
case R.id.no7: res.append("7");break;
case R.id.no8: res.append("8");break;
case R.id.no9: res.append("9");break;
case R.id.plus:
op='+';
num1= Float.parseFloat(res.getText().toString());
res.setText("");
break;
case R.id.min:
op='-';
num1= Float.parseFloat(res.getText().toString());
res.setText("");
break;
case R.id.mul:
op='x';
num1= Float.parseFloat(res.getText().toString());
res.setText("");
break;
case R.id.div:
op='/';
num1= Float.parseFloat(res.getText().toString());
res.setText("");
break;
case R.id.dot:{
if(!res.getText().toString().contains("."))
res.append(".");
else
//Toast.makeText(this,"ALREADY THERE ",Toast.LENGTH_SHORT).show();
break;}
case R.id.equ:
num2= Float.parseFloat(res.getText().toString());
switch (op){
case '+':
//Toast.makeText(this,"PAAGAL INSAAN ",Toast.LENGTH_SHORT).show();
num1=num1+num2;
System.out.println(num1);
res.setText((num1).toString());
break;
case '-':
num1=num1-num2;
res.setText((num1).toString());
System.out.println((num1));
break;
case 'x':
num1=num1*num2;
System.out.println((num1));
res.setText((num1).toString());
break;
case '/':
num1=num1/num2;
res.setText((num1).toString());
break;
default:break;
}
break;
case R.id.cancel:
// Toast.makeText(this,"CANCELLED",Toast.LENGTH_SHORT).show();
res.setText("");
break;
case R.id.back:
if(res.getText().toString().length()>0)
res.getText().delete(res.getText().toString().length()-1,res.getText().toString().length() );
else
Toast.makeText(this,"PAAGAL INSAAN ",Toast.LENGTH_SHORT).show();
/*
{Integer r=Integer.parseInt(res.getText().toString());
r=r/10;
if(r!=0)
res.setText(r.toString());
else
res.setText(" ");
}
*/
break;
default:break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:id='0'
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:hint="@string/edtext"
android:paddingRight="30sp"
android:layout_marginTop="5dp"
android:textSize="60sp"
android:id="@+id/res"
android:editable="false"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/button_clear"
android:onClick="buttonClick"
android:id="@+id/cancel"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="@string/button_back"
android:onClick="buttonClick"
android:id="@+id/back"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_div"
android:id="@+id/div"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/button_7"
android:onClick="buttonClick"
android:id="@+id/no7"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_8"
android:id="@+id/no8"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_9"
android:id="@+id/no9"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_mul"
android:id="@+id/mul"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_4"
android:id="@+id/no4"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_5"
android:id="@+id/no5"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_6"
android:id="@+id/no6"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_minus"
android:id="@+id/min"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_1"
android:id="@+id/no1"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_2"
android:id="@+id/no2"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_3"
android:id="@+id/no3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="buttonClick"
android:text="@string/button_0"
android:id="@+id/no0"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="@string/button_equals"
android:onClick="buttonClick"
android:id="@+id/equ"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/button_plus"
android:onClick="buttonClick"
android:id="@+id/plus"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<resources>
<string name="app_name">My Calculator</string>
<string name="action_settings">Settings</string>
<string name="abc">1</string>
<string name="button_1">1</string>
<string name="button_2">2</string>
<string name="button_3">3</string>
<string name="button_4">4</string>
<string name="button_5">5</string>
<string name="button_6">6</string>
<string name="button_7">7</string>
<string name="button_8">8</string>
<string name="button_9">9</string>
<string name="button_0">0</string>
<string name="button_plus">+</string>
<string name="button_minus">-</string>
<string name="button_mul">x</string>
<string name="button_div">/</string>
<string name="button_back">Backspace</string>
<string name="button_equals">=</string>
<string name="button_clear">C</string>
<string name="edtext"></string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment