Android Calculator Project:
MainActivity.java
packagecom.sattar.sattarcalculator;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.EditText;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclassMainActivityextends Activity {
EditTextinp1, inp2;
TextViewout;
@Override
protectedvoidonCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inp1 = (EditText)
findViewById(R.id.editText1);
inp2 = (EditText)
findViewById(R.id.editText2);
out = (TextView)
findViewById(R.id.textView1);
}
publicvoid add(View v) {
String
str1 = inp1.getText().toString();
String
str2 = inp2.getText().toString();
if (!str1.equals("") &&
!str2.equals("")) {
double inpt1 = Double.parseDouble(str1);
double inpt2 = Double.parseDouble(str2);
double result = inpt1 +
inpt2;
Toast.makeText(getApplicationContext(),
"Result=
" +
result,
Toast.LENGTH_LONG).show();
out.setText("Result=
" +
result);
}
else {
out.setTextColor(Color.RED);
out.setText("Please Enter
Both number...");
}
}
publicvoid subtract(View v) {
String
str1 = inp1.getText().toString();
String
str2 = inp2.getText().toString();
if (!str1.equals("") &&
!str2.equals("")) {
double inpt1 = Double.parseDouble(str1);
double inpt2 = Double.parseDouble(str2);
double result = inpt1 -
inpt2;
Toast.makeText(getApplicationContext(),
"Result=
" +
result,
Toast.LENGTH_LONG).show();
out.setText("Result=
" +
result);
}
else {
out.setTextColor(Color.RED);
out.setText("Please Enter
Both number...");
}
}
publicvoidmul(View v) {
String
str1 = inp1.getText().toString();
String
str2 = inp2.getText().toString();
if (!str1.equals("") &&
!str2.equals("")) {
double inpt1 = Double.parseDouble(str1);
double inpt2 = Double.parseDouble(str2);
double result = inpt1 *
inpt2;
Toast.makeText(getApplicationContext(),
"Result=
" +
result,
Toast.LENGTH_LONG).show();
out.setText("Result=
" +
result);
}
else {
out.setTextColor(Color.RED);
out.setText("Please Enter
Both number...");
}
}
publicvoid div(View v) {
String
str1 = inp1.getText().toString();
String
str2 = inp2.getText().toString();
if (!str1.equals("") &&
!str2.equals("")) {
double inpt1 = Double.parseDouble(str1);
double inpt2 = Double.parseDouble(str2);
double result = inpt1 /
inpt2;
Toast.makeText(getApplicationContext(),
"Result=
" +
result,
Toast.LENGTH_LONG).show();
out.setText("Result=
" +
result);
}
else {
out.setTextColor(Color.RED);
out.setText("Please Enter
Both number...");
}
}
}
Activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal">
<requestFocus/>
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="add"
android:layout_height="wrap_content"
android:text="+"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="subtract"
android:text="-"/>
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="mul"
android:layout_height="wrap_content"
android:text="*"/>
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="div"
android:layout_height="wrap_content"
android:text="/"/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"/>