Code :
calculator.java
package
com.example.devang.exhello;
import
android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class calculator extends
AppCompatActivity {
EditText txt;
Button
button0,button1,button2,button3,button4,button5,button6,button7,button8,button9;
Button
add,mul,div,min,sqr,root,clr,back,eql,dot;
double val1,val2;
boolean badd, bsub, bmul, bdiv;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
txt = findViewById(R.id.editText2);
add = findViewById(R.id.btn_add);
mul = findViewById(R.id.btn_mul);
div = findViewById(R.id.btn_div);
min = findViewById(R.id.btn_min);
sqr = findViewById(R.id.btn_sqrt);
root=findViewById(R.id.btn_root);
clr = findViewById(R.id.button9);
back=findViewById(R.id.button6);
eql = findViewById(R.id.btn_eql);
button0 = (Button) findViewById(R.id.btn_0);
button1 = (Button)
findViewById(R.id.btn_1);
button2 = (Button)
findViewById(R.id.btn_2);
button3 = (Button)
findViewById(R.id.btn_3);
button4 = (Button)
findViewById(R.id.btn_4);
button5 = (Button) findViewById(R.id.btn_5);
button6 = (Button)
findViewById(R.id.btn_6);
button7 = (Button)
findViewById(R.id.btn_7);
button8 = (Button)
findViewById(R.id.btn_8);
button9 = (Button)
findViewById(R.id.btn_9);
dot = (Button) findViewById(R.id.btn_dot);
clr.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText("");
}
});
back.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
String
str=txt.getText().toString();
if (str.length() >1 ) {
str = str.substring(0,
str.length() - 1);
txt.setText(str);
}
else if (str.length() <=1 )
{
txt.setText("");
}
}
});
button0.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"0");
}
});
button1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"1");
}
});
dot.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+".");
}
});
button2.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"2");
}
});
button3.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"3");
}
});
button4.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"4");
}
});
button5.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"5");
}
});
button6.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"6");
}
});
button7.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"7");
}
});
button8.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"8");
}
});
button9.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+"9");
}
});
//Button per =
(Button)findViewById(R.id.btn_);
add.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText("");
badd = true;
bsub = false;
bmul = false;
bdiv = false;
}
});
min.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText("");
bsub = true;
badd = false;
bmul = false;
bdiv = false;
}
});
div.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText("");
bdiv = true;
badd = false;
bsub = false;
bmul = false;
}
});
mul.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText("");
bmul = true;
badd = false;
bsub = false;
bdiv = false;
}
});
sqr.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText(
Double.toString(Math.sqrt(val1)));
}
});
root.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val1 =
Double.parseDouble(txt.getText()+"");
txt.setText(
Double.toString(Math.exp(val1)));
}
});
eql.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
val2 =
Float.parseFloat(txt.getText() + "");
double ans;
if (badd == true) {
ans = val1 + val2;
txt.setText((Double.toString(ans)));
badd = false;
}
if (bsub == true) {
ans = val1 - val2;
txt.setText((Double.toString(ans)));
badd = false;
}
if (bmul == true) {
ans = val1 * val2;
txt.setText((Double.toString(ans)));
badd = false;
}
if (bdiv == true) {
ans = val1 / val2;
txt.setText((Double.toString(ans)));
badd = false;
}
}
});
}
}
XML FILE
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
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:gravity="right"
android:orientation="vertical"
tools:context=".calculator">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Value"
android:inputType="number"
/>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="@+id/button6"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="98dp"
android:layout_height="wrap_content"
android:text="Back" />
<Button
android:id="@+id/button9"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLR" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_sqrt"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQR" />
<Button
android:id="@+id/btn_1x"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1/x" />
<Button
android:id="@+id/btn_root"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXP" />
<Button
android:id="@+id/btn_div"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_7"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="@+id/btn_8"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="@+id/btn_9"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="@+id/btn_div"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_6"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="@+id/btn_5"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/btn_4"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/btn_mul"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_3"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/btn_2"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/btn_1"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/btn_min"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_dot"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." />
<Button
android:id="@+id/btn_0"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="@+id/btn_eql"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<Button
android:id="@+id/btn_add"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
No comments:
Post a Comment