Android Buttons


Android Buttons


A button in an interface consists of text or an icon in form of an image, which results in a corresponding action once the user presses it or touches it.

Image Not Found

A button can be created in three different ways:

  • The first option to create the buttons will be with text. We have to use the Button class in this case. This can be done with the help of the code given below.
  • The second option will be to the used image instead of text. For this, we have to use the ImageButton class. This can be done with the code given below.
  • The third option will be to have both, text and image. In this case, we will use the same Button class, along with the attribute android:drawableLeft.

While working with buttons, we have to keep in mind that primarily we have to work with two classes, that is “Button” and “ImageButton”.

A button responds to a user click with an event, that is why it requires an event handler. This is taken care of by the android:onClick attribute that is associated with the Button element or class. In the demonstration below, it shows how the “sendMessage” activity will get triggered while a user clicks.

Here, the send Message is a function or activity that will be called upon once the user clicks.