Creating Simple Maths Application for Android
This tutorial helps in understanding of
1) Designing Android application
2) Creating Input Forms
3) Handling events and updating the view
Steps:
1) Download the ADT (Android Developer Toolkit) from http://developer.android.com/sdk
2) Unzip it to your favorite drive location
3) Start eclipse and follow the initials steps Described : http://developer.android.com/training/basics/firstapp/index.html
4) Design the page as follows:
5) activity_main.xml file should look like below:
<
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:text="@string/first" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="19dp"
android:text="@string/second" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="14dp"
android:text="@string/addButton"
android:onClick="add"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/textView1"
android:text="@string/subtractButton"
android:onClick="sub" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="@string/ResultString" />
</RelativeLayout>
6) strings.xml should look as follows:
<?
xml version="1.0" encoding="utf-8"?>
<
resources>
<string name="app_name">FirstApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="first">First Number</string>
<string name="second">Second Number</string>
<string name="addButton">Add</string>
<string name="subtractButton">Sub</string>
<string name="ResultString">Result</string>
</resources>
7) MainActivity.java
public
class MainActivity extends Activity {
private MathsService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
}
@Override
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;
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
builder.setMessage(
"Are you sure you want to exit?")
.setCancelable(
false)
.setPositiveButton(
"Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(
"No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void add(View view){
EditText editText1 = (EditText)findViewById(R.id.
editText1);
EditText editText2 = (EditText)findViewById(R.id.
editText2);
int first = Integer.parseInt(editText1.getText().toString());
int second = Integer.parseInt(editText2.getText().toString());
service = new MathsService(new Add());
int result = service.performMathOperation(first, second);
TextView resultTxt = (TextView)findViewById(R.id.
textView3);
resultTxt.setText(
"Result: "+result);
}
public void sub(View view){
EditText editText1 = (EditText)findViewById(R.id.
editText1);
EditText editText2 = (EditText)findViewById(R.id.
editText2);
int first = Integer.parseInt(editText1.getText().toString());
int second = Integer.parseInt(editText2.getText().toString());
service = new MathsService(new Sub());
int result = service.performMathOperation(first, second);
TextView resultTxt = (TextView)findViewById(R.id.
textView3);
resultTxt.setText(
"Result: "+result);
}
}
The service layer can be implemented as per your choice.
Result: