[Java for Android App Development] [==)BULLET(==]:
Works perfectly thank you.

Just tried to add dialog and clear, exit buttons but getting fc again. Please look into . I am really excited to make my first app.

Thank you once again

MainActivity.java

package com.ptx.calcplus;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
private EditText txtN1;
private EditText txtN2;
private EditText txtAn;
private Button Add, Sub, Div, Mul, Clr, Ext;
private AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
double a = 0, b = 0, c = 0;
[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Add = (Button) findViewById(R.id.btnAdd);
Sub = (Button) findViewById(R.id.btnSub);
Div = (Button) findViewById(R.id.btnDiv);
Mul = (Button) findViewById(R.id.btnMul);
Clr = (Button) findViewById(R.id.btnClr);
Ext = (Button) findViewById(R.id.btnExt);
txtN1 = (EditText) findViewById(R.id.txtNum1);
txtN2 = (EditText) findViewById(R.id.txtNum2);
txtAn = (EditText) findViewById(R.id.txtAns);
Add.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
a = Double.parseDouble(txtN1.getText().toString());
b = Double.parseDouble(txtN2.getText().toString());
if(txtN1.getText().toString().isEmpty()||txtN2.getText().toString().isEmpty()){
dlgAlert.setMessage("Please enter number in text box!");
dlgAlert.setTitle("Enter Number");
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else{
c=a+b;
txtAn.setText(""+c);
}
}
});
Sub.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
a = Double.parseDouble(txtN1.getText().toString());
b = Double.parseDouble(txtN2.getText().toString());
if(txtN1.getText().toString().isEmpty()||txtN2.getText().toString().isEmpty()){
dlgAlert.setMessage("Please enter number in text box!");
dlgAlert.setTitle("Enter Number");
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else{
c=a-b;
txtAn.setText(""+c);
}
}
});
Mul.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
a = Double.parseDouble(txtN1.getText().toString());
b = Double.parseDouble(txtN2.getText().toString());
if(txtN1.getText().toString().isEmpty()||txtN2.getText().toString().isEmpty()){
dlgAlert.setMessage("Please enter number in text box!");
dlgAlert.setTitle("Enter Number");
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else{
c=a*b;
txtAn.setText(""+c);
}
}
});
Div.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
a = Double.parseDouble(txtN1.getText().toString());
b = Double.parseDouble(txtN2.getText().toString());
if(txtN1.getText().toString().isEmpty()||txtN2.getText().toString().isEmpty()){
dlgAlert.setMessage("Please enter number in text box!");
dlgAlert.setTitle("Enter Number");
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else if(b==0)
{
dlgAlert.setMessage("Number can not be zero");
dlgAlert.setTitle("Divider Zero!");
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else{
c=a+b;
txtAn.setText(""+c);
}
}
});
Clr.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
txtN1.setText("");
txtN2.setText("");
txtAn.setText("");
}
});
Ext.setOnClickListener(new View.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}

[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

activity_main.xml

<RelativeLayout 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"
tools:context=".MainActivity" >

<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnSub"
android:layout_alignBottom="@+id/btnSub"
android:layout_alignParentLeft="true"
android:text="@string/btnm"
android:onClick="mul" />

<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="127dp"
android:layout_toRightOf="@+id/btnMul"
android:text="@string/btns"
android:onClick="sub" />

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnSub"
android:layout_alignBottom="@+id/btnSub"
android:layout_toRightOf="@+id/btnSub"
android:text="@string/btnp"
android:onClick="add" />

<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnAdd"
android:layout_alignBottom="@+id/btnAdd"
android:layout_toRightOf="@+id/btnAdd"
android:text="@string/btnd"
android:onClick="div"/>

<EditText
android:id="@+id/txtNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnMul"
android:layout_alignLeft="@+id/btnMul"
android:layout_alignRight="@+id/btnDiv"
android:layout_marginBottom="90dp"
android:ems="10"
android:hint="@string/txt2"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/txtNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txtNum2"
android:layout_alignLeft="@+id/txtNum2"
android:layout_alignRight="@+id/txtNum2"
android:layout_marginBottom="39dp"
android:ems="10"
android:hint="@string/txt1"
android:inputType="numberDecimal" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/txtAns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnMul"
android:ems="10"
android:enabled="false"
android:hint="@string/txta"
android:linksClickable="false" />

<Button
android:id="@+id/btnClr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnSub"
android:layout_below="@+id/txtAns"
android:layout_marginTop="21dp"
android:text="@string/btnc"
android:onClick="clr" />

<Button
android:id="@+id/btnExt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnClr"
android:layout_alignRight="@+id/txtAns"
android:text="@string/btne"
android:onClick="ext" />

</RelativeLayout>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ptx.calcplus"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ptx.calcplus.MainActivity"
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>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">CalcPlus</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="btnp">+</string>
<string name="btns">-</string>
<string name="btnm">*</string>
<string name="btnd">/</string>
<string name="txt2">Enter number 2 here</string>
<string name="txt1">Enter number 1 here</string>
<string name="txta">Result</string>
<string name="btnc">Clear</string>
<string name="btne">Exit</string>

</resources>

Please help me out!