Button
- A button is a Push- button which can be pressed , or clicked , by the user to perform some action
XML Attributes of Button Widget
XML Attributes | Description |
android:id | Used to specify the id of the view. |
android:text | Used to the display text of the button. |
android:textColor | Used to the display color of the text. |
android:textSize | Used to the display size of the text. |
android:textStyle | Used to the display style of the text like Bold, Italic, etc. |
android:textAllCaps | Used to display text in Capital letters. |
android:background | Used to set the background of the view. |
android:padding | Used to set the padding of the view. |
android:visibility | Used to set the visibility of the view. |
android:gravity | Used to specify the gravity of the view like center, top, bottom, etc |
Button code in XML file :
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" android:background="#168BC34A" tools:context=".MainActivity"> <!-- Button added in the activity --> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#4CAF50" android:paddingStart="10dp" android:paddingEnd="10dp" android:text="@string/btn" android:textColor="@android:color/background_light" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>