Android tutorial 1 – Basic Button

Last time I posted, it was with the iphone tutorial on how to make a iPhone app that changes a text field to a value once a button is pressed.

Well I am here once more, but this time it is with Android!

Now if you have not got it already, go and get the Android sdk from developer.android.com.
It has guides on how to install it on the 3 major OSs, also follow how to make a AVD (Android Virtual Device) as I will not be covering that in this tutorial.

Once all that has been done, launch your IDE (in 99% of cases it will be eclipse with the android plugin). And make a new project.
Fill out the data similiar to the picture
Screen shot 2010-05-11 at 11.00.55

Once that is done you should see your new project in the project explorer. Find main.java ( or what ever you called your activity, it will be in the same location )
Screen shot 2010-05-11 at 11.01.41

Once you find it, double click it and it will load the main activity file into the eclipse editor. Now to make those important changes ( If you followed my iPhone tutorial you will notice there is not as much commented code here. Google expect you right your own methods). As previously, all bolded text is text added.

package com.mybuttonisawesome;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {

Button myButton;
TextView myLabel;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myButton = (Button)findViewById(R.id.Button01);
myLabel = (TextView)findViewById(R.id.TextView01);

myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myLabel.setText("Moo");

}
});

}
}

If you notice a red underline under TextView or Button, hover your mouse over them and eclipse will suggest importing the needed file.
Screen shot 2010-05-11 at 11.22.28

Now to go to the ui editor, find main.xml in the folder Layouts, under Res in the project explorer.
Screen shot 2010-05-11 at 11.24.47

You should now see a black rectangle with “Hello world!” therein, now you have to delete this. Click on it and press delete on your keyboard, or right click then click remove.
Add a TextView and a Button to the black box from the side panel (under views) and thats it. You do not need to bind them like the iPhone.
Screen shot 2010-05-11 at 11.05.41

Launch your AVD and click Run then Run from the toolbar, (cmd + shift +f11 for mac). It should load after a while and when you click the button the textview text will change :D

Hope this help

Author:Alan Hamlyn

-- Alan Hamlyn Founder of Wuup

Subscribe

Subscribe to our e-mail newsletter to receive updates.

  • http://topsy.com/trackback?utm_source=pingback&utm_campaign=L2&url=http://www.wuup.co.uk/android-tutorial-1-basic-button Tweets that mention Android tutorial 1 – Basic Button | Wuup — Topsy.com

    [...] This post was mentioned on Twitter by Alan Hamlyn, Tammy Kahn Fennell, MarketMeTweet, Web Source Events, PieDog Media and others. PieDog Media said: RT @Wuup Android tutorial 1 – Basic Button http://bit.ly/aTvLn7 [...]

  • Abc

    Thanks for the beginning tutorial on button. Though, at first, the android emulator gave ‘…. stopped unexpected’ error; it was corrected after I clean the project and again run it.

  • http://www.facebook.com/quedlin László Oláh

    Thanks for reminding me to clean my project. I also had the “stpped unexpected error…” message, but now it works fine. Thank u guys!