Skip to content

Instantly share code, notes, and snippets.

@MohamedRamadanSaad
Created May 19, 2015 20:53
Show Gist options
  • Save MohamedRamadanSaad/1c38f6deca194c51a394 to your computer and use it in GitHub Desktop.
Save MohamedRamadanSaad/1c38f6deca194c51a394 to your computer and use it in GitHub Desktop.
Simple calculator , Dialog Activity , Android
package com.example.first;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
int one,two,flag,result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button plus=(Button)findViewById(R.id.plus_but);
Button minus=(Button)findViewById(R.id.min_but);
Button darb=(Button)findViewById(R.id.darb_but);
Button qisma=(Button)findViewById(R.id.qisma_but);
Button equal=(Button)findViewById(R.id.equal_but);
final EditText text=(EditText)findViewById(R.id.text1);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
one = Integer.parseInt(text.getText().toString());
flag=1;
text.setText("");
}
} );
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
one = Integer.parseInt(text.getText().toString());
flag=2;
text.setText("");
}
});
darb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
one = Integer.parseInt(text.getText().toString());
flag=3;
text.setText("");
}
});
qisma.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
one = Integer.parseInt(text.getText().toString());
flag=4;
text.setText("");
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
two = Integer.parseInt(text.getText().toString());
if (flag==1){
result=one+two;
Toast.makeText(MainActivity.this, "The result is : "+result, Toast.LENGTH_LONG).show();
text.setText("");
one=0;
two=0;
}else if(flag==2){
result=one-two;
Toast.makeText(MainActivity.this, "The result is : "+result, Toast.LENGTH_LONG).show();
text.setText("");
one=0;
two=0;
}else if(flag==3){
result=one*two;
Toast.makeText(MainActivity.this, "The result is : "+result, Toast.LENGTH_LONG).show();
text.setText("");
one=0;
two=0;
}else if(flag==4){
if(two==0){
Toast.makeText(MainActivity.this, "Can't Combine", Toast.LENGTH_LONG).show();
text.setText("");
one=0;
two=0;
}else {
result=one/two;
Toast.makeText(MainActivity.this, "The result is : "+result, Toast.LENGTH_LONG).show();
text.setText("");
one=0;
two=0;}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment