In this post, we will learn about Android Button widget. We will also see different attributes that are used to customise this widget.
Output
Android Button Tutorial Output
Video Output
Getting Started
Android Button can be defined as below –
Android Button is user interface that user can tap or click to perform some action.
Different Attributes Of Android Button Widget
Different attributes that are used in button are list below. However, if you want to see the complete list of attributes, you need to visit android official documentation on Button widget. Here, we are listing commonly used attributes.
Attributes in Button are inherited from TextView and View. Some of the popular attributes are –
Sr.
XML Attributes
Description
1
android:background
Sets background to this View.
2
android:backgroundTint
Sets tint to the background.
3
android:clickable
Sets true when you want to make this View clickable. Otherwise, set false.
4
android:drawableBottom
This is drawable to be drawn at bottom of the text.
5
android:drawableEnd
This is drawable to be drawn to end of the text.
6
android:drawableLeft
This is drawable to be drawn to left of the text.
7
android:drawablePadding
This is padding of the drawable.
8
android:drawableRight
This is drawable to be drawn to right of the text.
9
android:drawableStart
This is drawable to be drawn to start of the text.
10
android:drawableTop
This is drawable to be drawn at top of the text.
11
android:text
Sets the text of the EditText.
12
android:textAllCaps
Shows text in capital letters.
13
android:textColor
Sets color of the text.
14
android:textSize
Sets size of the text.
15
android:textStyle
Sets style of the text. For example, bold, italic, bolditalic etc.
16
android:typeface
Sets typeface of the text. For example, normal, sans, serif, monospace.
Example Of Android Button Widget
In this section, we will learn how to use android Button widget in any android application. At first, we will create android application. Then, we will use Button widget in the application.
Follow the steps below to create new android project. Please ignore the steps if you have already created the project.
1. Creating New Project
Step
Description
1.
Open Android Studio.
2.
Go to File => New => New Project. Write application name as Button. Then, click next button.
3.
Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4.
Then, select Empty Activity => click next => click finish.
5.
If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.
Since we have a project now. We will modify the xml and java file to use Button widget. Please follow the steps below.
2. Modify Values Folder
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">Button</string>
<string name="show_message">Show message</string>
<string name="welcome_message">Welcome to Button Tutorial!</string>
</resources>
Other values folder have not been changed. So, we are not going to mention it here.
Here, we have defined android Button widget in xml file. Now, we will access this Button in java file. Then, we will show Toast message on button click.
4. Access Button in java file
Open src/main/java/com.tutorialwing.edittext/MainActivity.java file. Then, add below code into it.
You must be logged in to post a comment.