How to Add a Button to an Activity, in XML

Button was added in API level 1

1. In the main.xml file, and add the below code below your layout tags. This will display a button.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="My Button Text"
    />

</LinearLayout>

2. Compile and run!

3. This will display a button, however nothing will happen when you click on the button yet though! To do that you'll need to add an onClick to your button.

Next Recommended Article: Add an onClick to a Button

Related Articles
Resources: