SIMPLE ADDING MACHINE (Beginner)
Tutorial is for people who already have eclipse installed and the SDK.
If not go here for info.
STEP1
Start up eclipse and start a new android project.
You can name your project add.
Then below that youll see properties
APPliCATION NAME: add
PACKAGE NAME:com.add
CREATE ACTIVITY:add
STEP2
In the res folder of the package explorer window youll see a layout folder. Enter a paste the following code into the main.xml file. You'll be pasting everything thus overwriting the previous code.
LAYOUT
Edit the XML file by cutting this code into it.
Code:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget41"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff66ffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/calBtn"
android:layout_width="96px"
android:layout_height="34px"
android:background="#ff0099ff"
android:text="Calculate"
android:textSize="18sp"
android:layout_x="104px"
android:layout_y="259px"
>
</Button>
<EditText
android:id="@+id/field1"
android:layout_width="167px"
android:layout_height="50px"
android:textSize="18sp"
android:layout_x="71px"
android:layout_y="45px"
>
</EditText>
<EditText
android:id="@+id/field2"
android:layout_width="164px"
android:layout_height="55px"
android:textSize="18sp"
android:layout_x="74px"
android:layout_y="183px"
>
</EditText>
<TextView
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="="
android:textSize="25sp"
android:textStyle="bold"
android:layout_x="133px"
android:layout_y="129px"
>
</TextView>
<TextView
android:id="@+id/answer"
android:layout_width="99px"
android:layout_height="29px"
android:textSize="19sp"
android:layout_x="124px"
android:layout_y="330px"
android:textColor="#ff000000">
</TextView>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="+"
android:layout_x="63px"
android:layout_y="128px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESULT:->"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_x="26px"
android:layout_y="333px"
>
</TextView>
<TextView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Operations:"
android:textSize="16sp"
android:layout_x="192px"
android:layout_y="81px"
>
</TextView>
<RadioButton
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="41px"
android:text=" +"
android:layout_x="32px"
android:layout_y="124px"
>
</RadioButton>
<RadioButton
android:id="@+id/Mul"
android:layout_width="96px"
android:layout_height="wrap_content"
android:text=" *"
android:layout_x="100px"
android:layout_y="122px"
>
</RadioButton>
<RadioButton
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" -"
android:layout_x="170px"
android:layout_y="120px"
>
</RadioButton>
<RadioButton
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" /"
android:layout_x="237px"
android:layout_y="120px"
>
</RadioButton>
<ImageView
android:id="@+id/widget41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_x="237px"
android:layout_y="38px"
>
</ImageView>
<TextView
android:id="@+id/widget42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold|italic"
android:layout_x="102px"
android:layout_y="9px" android:textColor="#ff000000" android:text="CALCULATE">
</TextView>
</AbsoluteLayout>
The layout file, is the file that defines the actual interface of the application.
STEP3
In the src folder paste the following code below in the source file.
CODE
Code:
/*Date:July 12 , 2010
*Program Developed by Andre Piper aka mobile_fan_2k5.
*email:andrepiper@gmail.com
*/
package com.Calculate;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class Calculate extends Activity implements OnClickListener
{
TextView answer;
Button calculate;
TextView number1,number2;
RadioButton plus,minus,divide,multi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//------------
plus=(RadioButton)this.findViewById((R.id.plus));
minus=(RadioButton)this.findViewById((R.id.minus));
divide=(RadioButton)this.findViewById((R.id.div));
multi=(RadioButton)this.findViewById((R.id.Mul));
answer=(TextView)this.findViewById((R.id.answer));
number1=(TextView)this.findViewById((R.id.field1));
number2=(TextView)this.findViewById((R.id.field2));
calculate=(Button)this.findViewById(R.id.calBtn);
calculate.setOnClickListener(this);
}
//-----BUTTON PRESSED
public void onClick(View v){
if(calculate.isPressed())
{
if(plus.isChecked())
{
plusFunc();
plus.setChecked(false);//uncheck button
number1.setText("");
number2.setText("");
}
//------------
if(minus.isChecked())
{
minusFunc();
minus.setChecked(false);//uncheck button
number1.setText("");
number2.setText("");
}
//-----------
if(divide.isChecked())
{
divideFunc();
divide.setChecked(false);//uncheck button
number1.setText("");
number2.setText("");
}
//--------------
if(multi.isChecked())
{
multiFunc();
multi.setChecked(false);//uncheck button
number1.setText("");
number2.setText("");
}
}
}
//--------------------Doing the actual adding,minus,divide and multiplaication
protected void plusFunc()
{
double value1=Double.parseDouble(number1.getText().toString());
double value2=Double.parseDouble(number2.getText().toString());
double answer2=value1+value2;
answer.setText(Double.toString(answer2));
}
protected void minusFunc()
{
double value1=Double.parseDouble(number1.getText().toString());
double value2=Double.parseDouble(number2.getText().toString());
double answer2=value1-value2;
answer.setText(Double.toString(answer2));
}
protected void divideFunc()
{
double value1=Double.parseDouble(number1.getText().toString());
double value2=Double.parseDouble(number2.getText().toString());
double answer2=value1/value2;
answer.setText(Double.toString(answer2));
}
protected void multiFunc()
{
double value1=Double.parseDouble(number1.getText().toString());
double value2=Double.parseDouble(number2.getText().toString());
double answer2=value1*value2;
answer.setText(Double.toString(answer2));
}
}
STEP4
Just run and you have an android app.
For a better tutorial check android sdk and android resources.