Download APK
MainActivity.java File:
package in.techlaugh.MyCalc;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView disp;
TextView dispop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(in.techlaugh.MyCalc.R.layout.activity_main);
disp = (TextView) findViewById(in.techlaugh.MyCalc.R.id.textView);
disp.setText("0.0");
dispop=(TextView)findViewById(in.techlaugh.MyCalc.R.id.textView3);
}
static boolean isempty=true;
public void numclick(View sender)
{
Button bt=(Button)sender;
if(disp.getText().length()>7) return;
if(isempty)
{
if(bt.getText().toString().equals("0")) return;
disp.setText(bt.getText());
isempty=false;
}
else
{
disp.append(bt.getText());
}
}
static float acc=0;
static short op=0;
static boolean finalflag=false;
public void opclick(View sender) {
Button bt=(Button)sender;
switch (op) {
case 0:
if (finalflag) {
acc=Float.parseFloat(disp.getText().toString());
finalflag=false;
}
else
{
acc += Float.parseFloat(disp.getText().toString());
}
break;
case 1:
if (acc == 0 || finalflag) {
acc = Float.parseFloat(disp.getText().toString());
finalflag=false;
} else {
acc -= Float.parseFloat(disp.getText().toString());
}
break;
case 2:
if (finalflag) {
acc=Float.parseFloat(disp.getText().toString());
finalflag=false;
}
else
{
acc *= Float.parseFloat(disp.getText().toString());
}
break;
case 3:
if (finalflag) {
acc=Float.parseFloat(disp.getText().toString());
finalflag=false;
}
else
{
acc /= Float.parseFloat(disp.getText().toString());
}
break;
}
disp.setText(Float.toString(acc));
if (bt.getText().toString().equals("+")) {op = 0; dispop.setText("+");}
if (bt.getText().toString().equals("-")) {op = 1; dispop.setText("-");}
if (bt.getText().toString().equals("*")) {op = 2; dispop.setText("*");}
if (bt.getText().toString().equals("/")) {op = 3; dispop.setText("/");}
isempty = true;
}
public void clear(View sender)
{
disp.setText("0.0");
acc=0;
isempty=true;
dispop.setText("");
}
public void finaloutput(View sender)
{
if(isempty)return;
if(op==0) {
disp.setText(Float.toString(acc + Float.parseFloat(disp.getText().toString())));
}
if(op==1) {
disp.setText(Float.toString(acc - Float.parseFloat(disp.getText().toString())));
}
if(op==2) {
disp.setText(Float.toString(acc * Float.parseFloat(disp.getText().toString())));
}
if(op==3) {
disp.setText(Float.toString(acc / Float.parseFloat(disp.getText().toString())));
}
dispop.setText("=");
finalflag=true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(in.techlaugh.MyCalc.R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == in.techlaugh.MyCalc.R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity_main.xml File:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="125dp"
android:onClick="numclick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/button2"
android:onClick="numclick"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button3"
android:onClick="numclick"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button4"
android:onClick="numclick"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/button5"
android:onClick="numclick"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/button6"
android:onClick="numclick"
android:layout_alignBottom="@+id/button5"
android:layout_toRightOf="@+id/button5"
android:layout_toEndOf="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button7"
android:onClick="numclick"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button8"
android:onClick="numclick"
android:layout_alignBottom="@+id/button7"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button9"
android:onClick="numclick"
android:layout_alignBottom="@+id/button8"
android:layout_toRightOf="@+id/button8"
android:layout_toEndOf="@+id/button8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="46dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/button10"
android:onClick="numclick"
android:layout_below="@+id/button7"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/button11"
android:onClick="numclick"
android:layout_alignBottom="@+id/button10"
android:layout_toRightOf="@+id/button10"
android:layout_toEndOf="@+id/button10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button13"
android:onClick="opclick"
android:layout_below="@+id/button10"
android:layout_alignLeft="@+id/button10"
android:layout_alignStart="@+id/button10" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/button14"
android:onClick="noclick"
android:layout_below="@+id/button12"
android:layout_alignLeft="@+id/button12"
android:layout_alignStart="@+id/button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/button12"
android:onClick="clear"
android:layout_below="@+id/button9"
android:layout_alignLeft="@+id/button9"
android:layout_alignStart="@+id/button9" />
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="="
android:id="@+id/button15"
android:onClick="finaloutput"
android:layout_alignBottom="@+id/button16"
android:layout_toRightOf="@+id/button16"
android:layout_toEndOf="@+id/button16" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/button16"
android:onClick="opclick"
android:layout_below="@+id/button14"
android:layout_toLeftOf="@+id/button14"
android:layout_toStartOf="@+id/button14" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/button17"
android:onClick="opclick"
android:layout_alignTop="@+id/button16"
android:layout_alignRight="@+id/button13"
android:layout_alignEnd="@+id/button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/button18"
android:onClick="opclick"
android:layout_alignBottom="@+id/button13"
android:layout_toRightOf="@+id/button13"
android:layout_toEndOf="@+id/button13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView3"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_toEndOf="@+id/button3" />
</RelativeLayout>
No comments:
Post a Comment