The button widget in Android can be used for various purposes. It totally depends on the requirements of your project. We often see buttons in the login and signup form. Android provides us wide range of properties to customize the button widget. Follow these steps to add a Button widget in Android activity.
Page Contents
- Add a Button in the XML layout file of yours activity.
- Add required attributes such as text, textColor, textSize, background to the Button widget.
- Open Kotlin file to modify attributes of textView and to set Listeners for the textView.
Adding Button in the layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="15dp"
android:paddingVertical="10dp"
android:text="Click Me"
android:textSize="18sp"
android:textColor="@color/white"
android:id="@+id/button"
android:background="@color/blue"
android:layout_centerInParent="true" />
</RelativeLayout>
In the above example, you can see we have added a Button widget in our XML file. Furthermore, we have also added some properties such as padding, textSize, textColor, and background to our Button widget. We have used white for the text color and blue for the background. you can add these two colors to your colors.xml file.
<color name="white">#FFFFFFFF</color>
<color name="blue">#304FFE</color>
List of available attributes for Button widget:
Attributes | Description |
---|---|
android:id | This attribute is used to set the id of the view. |
android:text | This attribute is used to the display text of the button. |
android:textColor | This attribute is used to the set color of the text. |
android:textSize | This attribute is used to the set size of the text. |
android:textStyle | This attribute is used to set the style of the text such as Bold, Italic, etc. |
android:textAllCaps | This attribute is used to set text in Capital letters such as true, false |
android:background | This attribute is used to set the background of the Button. |
android:padding | This attribute is used to set the padding of the Button. |
android:visibility | This attribute is used to set the visibility of the view such as visible, gone. |
android:gravity | This attribute is used to specify the gravity of the view such as center, top, bottom, etc |
After adding this widget into the XML file our next step is to set a listener to our button. so to do this we have to move to our Kotlin file and there we will set an onClickListerner for our button widget.
Set click listener to Button using Kotlin:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button) as Button
button.setOnClickListener(View.OnClickListener {
Toast.makeText(this@MainActivity, "You have Clicked on Button", Toast.LENGTH_SHORT).show()
})
}
}
As you can see in the above example program we have created a variable called the button and we assigned our XML widget to this variable with the help of findViewById method. After that, we have set a click listener to our button to show a toast message.
Output:
Here is the final output. when we click on the Button widget.