Prerequisite: Android ADT Plugin for Eclipse.
What we gonna have at the end is a calculator for Android. It will be able to summate, subtract, multiply and divide:
You need to write a number in the upper field and click a button for the operation you want. The result will be shown below the buttons.
Create your Android Project. In this case I named it Calculator and used the package de.duckout.calculator and choosed Android 2.3.3 as Build SDK and also Android 2.3.3 as Minimum Required SDK.
Strings:
As we use several strings, we start to create these.
In the Package Explorer go to “Calculator” -> “res” -> “values” -> “strings.xml”.
Choose the Graphical View. (You can always use the Graphical Interface and/or the XML View)
Press “Add” on the right side and create the following strings as shown in the next picture.
| Name | Value |
| plus | + |
| minus | - |
| multiply | * |
| divide | / |
Check the the xml view, which should look like this:
<resources>
<string name="app_name">Calculator</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">Duckout Calculator</string>
<string name="plus">+</string>
<string name="minus">-</string>
<string name="multiply">*</string>
<string name="divide">/</string>
</resources>
Layout
Navigate in the Package Explorer to “Calculator” -> “res” -> “layout” -> “activity_main.xml”.
You can choose between Graphical Layout and a XML View. As you see I used the Graphical Layout. To add a new object, just drag & drop it from the left side (“palette”) into the middle. But I recommend you to use the XML view, you can just copy the xml code below.
I added these objects:
| Object ID | Object Typ |
| number | EditText |
| tableRow1 | TableRow |
| plus | Button |
| minus | Button |
| divide | Button |
| multiply | Button |
| result | TextView |
Check the the xml view, which should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/number"
android:layout_marginLeft="0dp"
android:layout_marginTop="42dp" >
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plus"
android:onClick="onPlus" />
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minus"
android:onClick="onMinus" />
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/divide"
android:onClick="onDivide" />
<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multiply"
android:onClick="onMultiply" />
</TableRow>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@android:id/tabs" />
</RelativeLayout>
Implementation
Now navigate to “Calculator” -> “src” -> “de.duckout.calculator” -> “MainActivity.java”. See the following coding which is commented a lot and implements the calculation logic.
package de.duckout.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText inputNumber;
private TextView outputResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// These Button have been initialized in the activity_main.xml
inputNumber = (EditText) findViewById(R.id.number);
outputResult = (TextView) findViewById(R.id.result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// This handles the click of the plus button
// we specified this method in the activity_main.xml
public void onPlus(View view) {
float res = this.getInputNumber() + this.getCurrentResult();
this.outputResult.setText(String.valueOf(res));
}
// This handles the click of the minus button
// we specified this method in the activity_main.xml
public void onMinus(View view) {
float res = this.getCurrentResult() - this.getInputNumber();
this.outputResult.setText(String.valueOf(res));
}
// This handles the click of the divide button
// we specified this method in the activity_main.xml
public void onDivide(View view) {
// you shouldn't divide with 0
if (this.getInputNumber() > 0) {
float res = this.getCurrentResult() / this.getInputNumber();
this.outputResult.setText(String.valueOf(res));
}
}
// This handles the click of the multiply button
// we specified this method in the activity_main.xml
public void onMultiply(View view) {
float res = this.getInputNumber() * this.getCurrentResult();
this.outputResult.setText(String.valueOf(res));
}
// returns the input number
private float getInputNumber() {
if (this.inputNumber.getText().length() == 0) {
return 0;
}
// the input is given through the .getText() method of the Edittext
return Float.parseFloat(this.inputNumber.getText().toString());
}
// returns the current result
private float getCurrentResult() {
if (this.outputResult.getText().length() == 0) {
return 0;
}
// the result is given through the .getText() method of the TextView
return Float.parseFloat(this.outputResult.getText().toString());
}
}
Sources: Android Developers
Der Beitrag Android Calculator erschien zuerst auf Duckout.


