Android button tutorial
Saturday, January 31st, 2009 - 9:20 pm - Android
A lot of people have found this site by searching for an Android button tutorial, so here it is.
- This tutorial assumes that you already have an activity and are using an XML layout.
- Open the layout XML and add the button element. Assign an ID with the “@+id” operator. The + tells Android to generate an ID for this element so that you can reference it in your Java files.
- This is an example. Your layout and text elements will probably be very different. In this example case, the ID of the button is “close”.
<Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/title_close" />
- This is an example. Your layout and text elements will probably be very different. In this example case, the ID of the button is “close”.
- Open the activity class. Add a class property to hold a reference to the button.
private Button closeButton;
- If you haven’t already, override the onCreate method.
@Override protected void onCreate(Bundle savedInstanceState) { }
- For this example, we don’t need the saved instance state, so ignore it.
- Now, in the onCreate method, attach a listener to the click event for the button. This example will call “finish()” on the activity, the Android analog of clicking the close button on a window.
- Here’s a short description of what’s happening.
- First, get the button ID. The ID created earlier in the layout, “close”, is compiled by Android and assigned a unique integer ID which is available to the application through the “R” class, which I assume is short for “Resources”.
- Request a reference to the button from the activity by calling “findViewById”. The button has to be retrieved from the activity because while an ID is unique in an activity, it is not unique among all activities.
- Assign the retrieved button to an instance variable so that if you need it later, you can easily find it without having to query for it again.
- Create a class implementing “OnClickListener” and set it as the on click listener for the button.
As UI elements go, buttons are some of the simplest. Later, I’ll write about menus and dialogs, which aren’t so easy.
I am trying to figure out how to make and lay out buttons for the Android screen without making them in XML. Is this possible, and if so, how?
This website is great. I like it.(www.linkspirit.net)N_X_D_S.
this.buttonExeB = (Button)this.findViewById(R.id.buttonExe)
this.buttonExeB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
Doesnt work 🙁
Hi!
I would like to know the simplest explanation of how to make a simple button(widget) in layer1 that will lead to layer2 when pressed.
Please help!
I tried to google it but no luck, everyone likes to complicate things that you can´t understand.
Hello Friend
Can u anyone tell me how to redirect the page on click the button… i have develop 2 applications
* app1
* app2
when i click the button in app1 i want to open the second app(app2)
how can i give to a button a pen with color red
Im sandhya how to create a image in a classs
hi ,when orientation of sceen changed ,haw can display athor layout with xml
please help 🙂
this is a very helpful tip for the android development beginers thanks 🙂
hello everyone.
i m getting error again nd again. can u please help me.
“Unfortunately, E-Book has stopped”
how can i transfer command from one page to another page using button please help me.
Hey,
Nice blog!!
I think shipra , you should use intent inside the button click event for traversing from one page to another.
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this,destination.class);
startActivity(intent);
}
});
Hope this was helpful.. I’ve also discussed about intents in my humble blog http://androiddesk.wordpress.com/2011/12/24/explicit-intent/
May be you can go through it if you find it useful.
Thanks and Regards
Deepthi
Im getting error messages in eclipse telling me that I need to remove the @Override above:
public void onClick(View v) {
finish();
}
because it must override a superclass method…
Try changing to Java 1.6 instead of 1.5 from eclipse. That should solve this issue.
Just remove the @Override above:
public void onClick(View v) {
finish();
}
and make sure that your class should implement the interface onClickListener
Txs!
Thanks for the really simple description. It really helped to clear some things up that I was struggling to get my head round!
Hey this tutorial is quite helpful and works for one Button. But when I add a second one the App crashes, can you tell me what I am doing wrong?
public class RelationshipActivity extends Activity {
/** Called when the activity is first created. */
private Button OptionsButton;
private Button SaveButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SaveButton = (Button)findViewById(R.id.btnSave);
OptionsButton = (Button)findViewById(R.id.btnOptions);
SaveButton.setOnClickListener(new OnClickListener(){
public void onClick(View save) {
setContentView(R.layout.main);
}
});
OptionsButton.setOnClickListener(new OnClickListener(){
public void onClick(View options) {
setContentView(R.layout.options);
}
});
}
}
this is my code….
public class mainmenu extends Activity {
/** Called when the activity is first created. */
public Button closeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.options);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
but i m getting error for setOnClickListener saying this method is not applicable!!! nd options.xml is the page i want to link to..
PLZ HELP!!!
please… finish(); ist just an example!
Change the line:
finish();
into:
// place your code here
and it will run
Instead of new onClickListener(), it has to be new View.onClickListener(). Just spent 30 minutes trying to figure out the exact same error. Hope this helps!