Defining and inflating a layout
When using the Android Studio wizard to create a new project, it automatically creates the res/layout/activity_main.xml
file (as shown in the following screenshot). It then inflates the XML file in the onCreate()
callback with setContentView(R.layout.activity_main)
.
For this recipe, we will create two, slightly different layouts and switch between them with a button.
Getting ready
Create a new project in Android Studio and call it InflateLayout
. Once the project is created, expand the res/layout
folder so we can edit the activity_main.xml
file.
How to do it...
Edit the
res/layout/activity_main.xml
file so it includes a button as defined here:<Button android:id="@+id/buttonLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left Button" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:onClick="onClickLeft"/>
Now make a copy of
activity_main.xml
and call it...