Jump to content
xisto Community
Sign in to follow this  
retry56

Tutorial Android Programming

Recommended Posts

Level: Easy

Material: - Using EditText, Button, TextView, String to Integer conversion with parseInt, conversion String with toString.

 

Posted Image


1. Open Eclipse [/ b], create a new project adjusted as shown below:

 

Posted Image

 

 

2. Click Finish, on the left hand side go into the folder layout select main.xml [/ b]

 

Posted Image

 

 

3. That we will create is to make 2 widgets EditText [/ i] as input integer, 4 Button [/ i] for the +, -, *, / and a TextView [/ i] for displays the results of the calculation.

 

<? 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"    >    <EditText    android: id = "@ + id/input1"    android: layout_width = "fill_parent"    android: layout_height = "wrap_content"    android: numeric = "integer"    android: text = "0"    />    <EditText    android: id = "@ + id/input2"    android: layout_width = "fill_parent"    android: layout_height = "wrap_content"    android: numeric = "integer"    android: text = "0"    /><LinearLayoutandroid: layout_width = "fill_parent"android: layout_height = "wrap_content"><Buttonandroid: id = "@ + id / add"android: layout_width = "fill_parent"android: layout_height = "wrap_content"android: layout_weight = "1"android: text = "+"> </ Button><Buttonandroid: id = "@ + id / less"android: layout_width = "fill_parent"android: layout_height = "wrap_content"android: layout_weight = "1"android: text = "-"> </ Button><Buttonandroid: id = "@ + id / time"android: layout_width = "fill_parent"android: layout_height = "wrap_content"android: layout_weight = "1"android: text = "*"> </ Button><Buttonandroid: id = "@ + id / for"android: layout_width = "fill_parent"android: layout_height = "wrap_content"android: layout_weight = "1"android: text = "/"> </ Button></ LinearLayout><TextViewandroid: id = "@ + id / results"    android: layout_width = "fill_parent"    android: layout_height = "wrap_content"    android: gravity = "center"    android: text = "Results"    />           <! - DiskusiAndroid.com ->    <TextView    android: layout_width = "fill_parent"    android: layout_height = "wrap_content"    android: layout_marginTop = "250px"    android: gravity = "center"    android: text = "diskusiAndroid.com"    />        <! - DiskusiAndroid.com ->    </ LinearLayout>

 

 

4. After setting the display main.xml , now set the code to hook Main.java with main.xml

. Open Main.java .

 

5. The first do is create a variable of each widget that we have created is Button , EditText and Textview . Create a variable like the following code:

 

EditText input1, input2;Button added, less, times, for;TextView results;    @ Override    public void OnCreate (Bundle savedInstanceState) {        super.onCreate (savedInstanceState);        setContentView (R.layout.main);                input1 = (EditText) findViewById (R.id.input1);        input2 = (EditText) findViewById (R.id.input2);        result = (TextView) findViewById (R.id.hasil);        added = (Button) findViewById (R.id.tambah);        less = (Button) findViewById (R.id.kurang);        time = (Button) findViewById (R.id.kali);        for = (Button) findViewById (R.id.bagi); [/ code] [/ spoiler]6. Because the application will work if we click the button +,-,*,/ then each widget [i] Button [/ i] should be in [b] setOnClickListener [/ b].[Spoiler] [code] tambah.setOnClickListener (this);        kurang.setOnClickListener (this);        kali.setOnClickListener (this);        bagi.setOnClickListener (this); [/code] [/spoiler]7. After adding [b] setOnClickListener [/b] do not forget to implement [b] OnClickListener [/ b].[spoiler] [code] public class Main extends Activity implements OnClickListener {[/code] [/spoiler]8. Then create the function [b] onClick [/b] for the button can be clicked.[spoiler] [code] public void onClick (View v) {/ / TODO Auto-generated method stubint one = Integer.parseInt (input1.getText (). toString ());int two = Integer.parseInt (input2.getText (). toString ());switch (v.getId ()) {R.id.tambah case:hasil.setText (Integer.toString (one + two));break;R.id.kurang case:hasil.setText (Integer.toString (one-two));break;R.id.kali case:hasil.setText (Integer.toString (one * two));break;R.id.bagi case:hasil.setText (Double.toString (one / two));break;}}

 

 

In the above code we first create an integer variable one and two. Because the input of EditText [/ i] form of String so it must be converted to integer with parseInt. Use the switch to set any button to be clicked in accordance +,-,*,/. button Before showing the results of the calculations, it should change back the results of the calculations in the form of an integer to a string by toString ();.

 

Posted Image


Edited by retry56 (see edit history)

Share this post


Link to post
Share on other sites

First advice that you need Android SDK installed , Java installed , Eclipse installed , Android SDK platforms downloaded.Also you will need to set up where is your Android SDK located , to make PATH=Java location , to select a Android SDK (for example 2.3) platform.http://www.makeuseof.com/tag/write-google-android-application/ I would suggest this tutorial which explains everything which can be pain-in-the-*bottom* if you don't follow tutorials and just view the android.com one :DPS. Why DiskusAndroid.com isn't working? The one advertised on your signature :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.