Pr.Pg Next Pg

Buttons tutorials

Adding Button

  • In order to add a button we define button in the main.xml file inside the linear layout.

  • We add various necessary attribute for now, like the button height, width and text size and then later on we will also assign IDs to our elements.

  • The following screenshot shows the state of the main.xml file after adding the button

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello"

android:textSize="40sp"

android:gravity="center"/>

 

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:layout_gravity="center"

android:gravity="center"/>

 

</LinearLayout>

 

 

  • After adding the above code in the XML file we can see the button in the graphical layout.

 

 

 

Pr.Pg border                                              Next Pg