This could be your first android project if you are learning. This demonstrates the following
How to create a layout with textviews buttons linear layout vertical horizontal how to gather controls how to setup buttons how to respond to buttons how to read/update edit text controls
satya - Friday, September 16, 2011 10:42:02 AM
Start with the layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:text="0"
android:inputType="numberDecimal"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/editText2"
android:inputType="numberDecimal">
</EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="+" android:id="@+id/plusButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="-" android:id="@+id/minusButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="*" android:id="@+id/multiplyButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="/" android:id="@+id/divideButton"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
satya - Friday, September 16, 2011 10:42:29 AM
Ofourse the background impate you will need to procure
and place it in the drawable directory.
satya - Friday, September 16, 2011 10:43:24 AM
A single activity can do the rest
package com.ai.teach.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CalculatorMainActivity extends Activity
implements OnClickListener
{
private EditText number1EditText;
private EditText number2EditText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gatherControls();
setupButtons();
}
private void gatherControls()
{
number1EditText = (EditText)this.findViewById(R.id.editText1);
number2EditText = (EditText)this.findViewById(R.id.editText2);
number2EditText.requestFocus();
}
private void setupButtons()
{
Button b = (Button)this.findViewById(R.id.plusButton);
b.setOnClickListener(this);
b = (Button)this.findViewById(R.id.minusButton);
b.setOnClickListener(this);
b = (Button)this.findViewById(R.id.multiplyButton);
b.setOnClickListener(this);
b = (Button)this.findViewById(R.id.divideButton);
b.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
String sNum1 = number1EditText.getText().toString();
String sNum2 = number2EditText.getText().toString();
double num1 = getDouble(sNum1);
double num2 = getDouble(sNum2);
Button b = (Button)v;
double value = 0;
if (b.getId() == R.id.plusButton)
{
value = plus(num1, num2);
}
else if (b.getId() == R.id.minusButton)
{
value = minus(num1, num2);
}
else if (b.getId() == R.id.multiplyButton)
{
value = multiply(num1, num2);
}
else if (b.getId() == R.id.divideButton)
{
value = divide(num1, num2);
}
number1EditText.setText(Double.toString(value));
}
private double plus(double n1, double n2)
{
return n1 + n2;
}
private double minus(double n1, double n2)
{
return n1 - n2;
}
private double multiply(double n1, double n2)
{
return n1 * n2;
}
private double divide(double n1, double n2)
{
if (n2 == 0)
{
return 0;
}
return n1 / n2;
}
private double getDouble(String s)
{
if (validString(s))
{
return Double.parseDouble(s);
}
return 0;
}
private boolean invalidString(String s)
{
return !validString(s);
}
private boolean validString(String s)
{
if (s == null)
{
return false;
}
if (s.trim().equalsIgnoreCase(""))
{
return false;
}
return true;
}
}
satya - Friday, September 16, 2011 10:45:06 AM
Here is the android manifest.xml for the full measure
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ai.teach.calculator"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".CalculatorMainActivity"
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>
This is unchanged from automatically created android project.
satya - Friday, September 16, 2011 10:50:52 AM
You can download the whole project here: (zip file)
You can download the whole project here. Please note that this is a zip file.
satya - 3/16/2014 11:46:36 AM
Here is how it looks like