Android Project: Calculator Project:
Android Project: Calculator Project
>>>Create a Java Class named
MainActivity.java & Paste my code
package com.sattar.calculator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText
inp1, inp2;
TextView
out;
@Override
protected
void onCreate(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);
}
public
void 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...");
}
}
public
void 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...");
}
}
public
void mul(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...");
}
}
public
void 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...");
}
}
}
>>> Create an Xml file named activity_main
<LinearLayout xmlns: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:hint="Input 1st number:"
android:inputType="numberDecimal"
android:textColorHint="@android:color/holo_orange_dark"
android:textStyle="bold" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Input 2nd Number:"
android:inputType="numberDecimal"
android:textColorHint="@android:color/holo_orange_dark"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:gravity="center_vertical" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="add"
android:text="+" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="subtract"
android:text="-" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="mul"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="div"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Result:"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
If
you have faced any problem comment it, I will try to solution it, thanks.
No comments:
Post a Comment