Introduction

Figure: Illustration of how ViewGroup objects form branches in the layout and contain other View objects.
Create a Linear Layout
- In Android Studio the activity_main.xml file can be found in the res/layout directory. However when the project is created you might have chosen blank activity now it contains RelativeLayout root view.
- Delete the <TextView> element
- Change the <RelativeLayout> element to <LinearLayout> because we are creating a linear layout.
- Add the android:orientation attribute and set it to "horizontal" and remove all the other attributes such as android: padding.
The resulting code after applying the preceding procedure will look as in the following:

Adding a Text Field
- In the activity_main.xml file within the <LinearLayout> element, define the new element <EditText> with the additional attribute "id".
- Define the layout_width and layout_height attributes as wrap_content.
- Define a "hint" attribute as a string object named edit_message.
The code will look like this after the addition of an <EditText> element as shown below.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <EditText
- android:id="@+id/edit_message"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="@string/edit_message" />
- </LinearLayout>


Comments
Join the conversation! Your thoughts help the community grow.