Android button tutorial

A lot of people have found this site by searching for an Android button tutorial, so here it is.

  1. This tutorial assumes that you already have an activity and are using an XML layout.
  2. 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.
    1. 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" />
  3. Open the activity class.  Add a class property to hold a reference to the button.
    private Button closeButton;
  4. If you haven’t already, override the onCreate method.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    }

    1. For this example, we don’t need the saved instance state, so ignore it.
  5. 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.
protected void onCreate(Bundle savedInstanceState) {
  this.setContentView(R.layout.layoutxml);
  this.closeButton = (Button)this.findViewById(R.id.close);
  this.closeButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      finish();
    }
  });
}
  1. Here’s a short description of what’s happening.
    1. 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”.
    2. 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.
    3. 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.
    4. 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.


  1. Joe's Morgue says:

    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?

  2. - says:

    This website is great. I like it.(www.linkspirit.net)N_X_D_S.

  3. Solo says:

    this.buttonExeB = (Button)this.findViewById(R.id.buttonExe)
    this.buttonExeB.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    finish();
    }
    });

    Doesnt work 🙁

  4. Kreso says:

    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.

  5. karthick vk says:

    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)

  6. xnadroid says:

    how can i give to a button a pen with color red

  7. sandhya says:

    Im sandhya how to create a image in a classs

  8. P.ck says:

    hi ,when orientation of sceen changed ,haw can display athor layout with xml
    please help 🙂

  9. himanshu says:

    this is a very helpful tip for the android development beginers thanks 🙂

  10. Shipra says:

    hello everyone.
    i m getting error again nd again. can u please help me.
    “Unfortunately, E-Book has stopped”

  11. Shipra says:

    how can i transfer command from one page to another page using button please help me.

    • Deepthi G says:

      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

  12. Alfy says:

    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…

    • Sarwar says:

      Try changing to Java 1.6 instead of 1.5 from eclipse. That should solve this issue.

    • wesagn says:

      Just remove the @Override above:
      public void onClick(View v) {
      finish();
      }

      and make sure that your class should implement the interface onClickListener
      Txs!

  13. androidbloke says:

    Thanks for the really simple description. It really helped to clear some things up that I was struggling to get my head round!

  14. Kevin says:

    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);
    }
    });
    }
    }

  15. Huzefa says:

    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!!!

    • Andi says:

      please… finish(); ist just an example!

      Change the line:
      finish();
      into:
      // place your code here
      and it will run

    • Ryan says:

      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!

Leave a Reply