Inserting a widget into a layout
As you may have seen from previous recipes, widgets are declared in a layout file, or created in code. For this recipe, we will go step-by-step to add a button with the Android Studio Designer. (For later recipes, we will just show the layout XML from the TextView.) After creating the button, we will create an onClickListener()
.
Getting ready
Start a new project in Android Studio and call it InsertWidget
. Use the default options for creating a Phone and Tablet project and select Empty Activity when prompted for the Activity Type. You can delete the default TextView (or leave it) as it will not be needed for this recipe.
How to do it...
To insert a widget into a layout, follow these steps:
- Open the activity_main.xml file in Android Studio and click on the Design tab.
- Find Button in the widget list and drag it to the center of the activity screen on the right. Android will automatically set the layout parameters based on where the button is dropped. If you center...