Last Updated:

button in Android - Example of button widget

In this article, we will learn how to use the widget in Android. It also shows you how to handle clicks, apply styles, work with the xml layout of a button, and manipulate the widget in code. The lesson will be useful for beginner Android programmers.ButtonButton

Today, we'll learn how to set different layouts for buttons, change background images, and handle widget-related events.Button

Create a button in Android

The button is a clickable Android widget that can take various forms and respond to user actions, namely: normal press, long press, etc. The widget is available in the package.Buttonandroid.widget.Button

Like any other widget, you can declare in the layout of your activity or snippet, also create a button programmatically.Button


Let's create the first buttons and set the different properties of each:


How do the buttons work? What happens in each Button widget?

Android renders the above layout XML as follows:

  1. Android will see the type of layout will set it to an upright position.LinearLayout
  2. The parameter specifies the width of the layout and the height. If in any of the values there is , then the button will stretch to the full screen; will indicate that the size of the button should be sufficient to accommodate the nested content (text, picture). layout_widthlayout_heightmatch_parentwrap_content
  3. android:layout_gravity determine the positioning of the widget relative to the parent component.Button
  4. android:gravity defines the content alignment . It is given by one of the constants .Buttonleft, right, center_vertical, center_horizontal, center

What does the layout look like after rendering (drawing the layout after analysis):


Initializing a button in Android

Now that we have a ready-made layout with four buttons of different shapes and sizes, let's initialize them in our code:Activity

Explanation: in the method, we find the identifier in the layout - and using type conversion indicate that this is a widget.onCreate()viewfindViewById(

Handling button events (clicks)

In order for our button to handle events, we need to call an interface method to trigger a callback to click the button:setOnClickListener()View.OnClickListener


Apply styles to an Android Button widget

Styles such as color, font, background, etc. can be applied to a button.Button

  1. The attribute sets the background of the button. It can be a hexadecimal color in the format or it can be an
  2. The a attribute allows you to set the color of the widget text.ndroid:textColorButton

Learn more about styles and themes in Android.

Let's remove the unused buttons and experiment a bit with our topButton button:

As you can see from the layout, we manually set the dimensions of the widget and , and also added the top indentation. We also used an attribute to set the background of the widget. Please note that the picture facebook.jpg we previously copied to the folder of the Android project.


If you don't understand some parts of the xml layout or code, that's fine. With each lesson, we will learn more and more and little by little the overall picture will be formed. In the meantime, stay tuned to the Android Development section.