SIMPLE CALCULATOR DEMO
CODE OF JAVA FILE IN ANDROID:-
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
private EditText input1;
private EditText input2;
private EditText solution;
private TextView operator;
private Calculator mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
input1 = (EditText) findViewById(R.id.input1);
input2 = (EditText) findViewById(R.id.input2);
solution = (EditText) findViewById(R.id.solution);
operator = (TextView) findViewById(R.id.operator);
// We create an OnClick Event in each button.
Button plusButton = (Button) findViewById(R.id.plusbutton);
Button minusButton = (Button) findViewById(R.id.minusbutton);
Button prodButton = (Button) findViewById(R.id.prodbutton);
Button divButton = (Button) findViewById(R.id.divbutton);
Button equalButton = (Button) findViewById(R.id.equalbutton);
plusButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("+");
}
});
minusButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("-");
}
});
prodButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("*");
}
});
divButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("/");
}
});
equalButton.setOnClickListener(new OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
if ((input1.getText().length() == 0)
|| (input1.getText().toString() == " ")
|| (input2.getText().length() == 0)
|| (input2.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("Some inputs are empty")
.setPositiveButton("OK", null).show();
} else if (operator.getText().equals("")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("Operator is null").setPositiveButton(
"OK", null).show();
} else if (operator.getText().equals("+")) {
double result = new Double(input1.getText().toString())
+ new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("-")) {
double result = new Double(input1.getText().toString())
- new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("*")) {
double result = new Double(input1.getText().toString())
* new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("/")) {
double result = new Double(input1.getText().toString())
/ new Double(input2.getText().toString());
solution.setText(Double.toString(result));
}
}
});
}
}
XML FILE CODE :-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget45"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="@+id/divbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="/"
android:layout_x="192px" android:layout_y="86px">
</Button>
<Button android:id="@+id/minusbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="-"
android:layout_x="254px" android:layout_y="18px">
</Button>
<EditText android:id="@+id/input1" android:layout_width="160px"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric = "decimal|signed"
android:layout_x="8px" android:layout_y="13px">
</EditText>
<EditText android:id="@+id/input2" android:layout_width="156px"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric = "decimal|signed"
android:layout_x="10px" android:layout_y="109px">
</EditText>
<Button android:id="@+id/plusbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="+"
android:layout_x="193px" android:layout_y="19px">
</Button>
<TextView android:id="@+id/operator" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="33px"
android:layout_y="77px">
</TextView>
<EditText android:id="@+id/solution" android:layout_width="304px"
android:layout_height="wrap_content" android:textSize="18sp"
android:layout_x="7px" android:layout_y="214px">
</EditText>
<Button android:id="@+id/prodbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="*"
android:layout_x="253px" android:layout_y="87px">
</Button>
<Button android:id="@+id/equalbutton" android:layout_width="120px"
android:layout_height="wrap_content" android:text="="
android:layout_x="192px" android:layout_y="149px">
</Button>
</AbsoluteLayout>
MANIFEST FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bright.hub.Calculator"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Calculator"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
STRING FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Calculator</string>
<string name="app_name">Calculator</string>
</resources>
RUN THE PROJECT OR ENJOY IT.
THANKS.
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
private EditText input1;
private EditText input2;
private EditText solution;
private TextView operator;
private Calculator mContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
input1 = (EditText) findViewById(R.id.input1);
input2 = (EditText) findViewById(R.id.input2);
solution = (EditText) findViewById(R.id.solution);
operator = (TextView) findViewById(R.id.operator);
// We create an OnClick Event in each button.
Button plusButton = (Button) findViewById(R.id.plusbutton);
Button minusButton = (Button) findViewById(R.id.minusbutton);
Button prodButton = (Button) findViewById(R.id.prodbutton);
Button divButton = (Button) findViewById(R.id.divbutton);
Button equalButton = (Button) findViewById(R.id.equalbutton);
plusButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("+");
}
});
minusButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("-");
}
});
prodButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("*");
}
});
divButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
operator.setText("/");
}
});
equalButton.setOnClickListener(new OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
if ((input1.getText().length() == 0)
|| (input1.getText().toString() == " ")
|| (input2.getText().length() == 0)
|| (input2.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("Some inputs are empty")
.setPositiveButton("OK", null).show();
} else if (operator.getText().equals("")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("Operator is null").setPositiveButton(
"OK", null).show();
} else if (operator.getText().equals("+")) {
double result = new Double(input1.getText().toString())
+ new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("-")) {
double result = new Double(input1.getText().toString())
- new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("*")) {
double result = new Double(input1.getText().toString())
* new Double(input2.getText().toString());
solution.setText(Double.toString(result));
} else if (operator.getText().equals("/")) {
double result = new Double(input1.getText().toString())
/ new Double(input2.getText().toString());
solution.setText(Double.toString(result));
}
}
});
}
}
XML FILE CODE :-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget45"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="@+id/divbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="/"
android:layout_x="192px" android:layout_y="86px">
</Button>
<Button android:id="@+id/minusbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="-"
android:layout_x="254px" android:layout_y="18px">
</Button>
<EditText android:id="@+id/input1" android:layout_width="160px"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric = "decimal|signed"
android:layout_x="8px" android:layout_y="13px">
</EditText>
<EditText android:id="@+id/input2" android:layout_width="156px"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric = "decimal|signed"
android:layout_x="10px" android:layout_y="109px">
</EditText>
<Button android:id="@+id/plusbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="+"
android:layout_x="193px" android:layout_y="19px">
</Button>
<TextView android:id="@+id/operator" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_x="33px"
android:layout_y="77px">
</TextView>
<EditText android:id="@+id/solution" android:layout_width="304px"
android:layout_height="wrap_content" android:textSize="18sp"
android:layout_x="7px" android:layout_y="214px">
</EditText>
<Button android:id="@+id/prodbutton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="*"
android:layout_x="253px" android:layout_y="87px">
</Button>
<Button android:id="@+id/equalbutton" android:layout_width="120px"
android:layout_height="wrap_content" android:text="="
android:layout_x="192px" android:layout_y="149px">
</Button>
</AbsoluteLayout>
MANIFEST FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bright.hub.Calculator"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Calculator"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
STRING FILE CODE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Calculator</string>
<string name="app_name">Calculator</string>
</resources>
RUN THE PROJECT OR ENJOY IT.
THANKS.
No comments: