I just started creating android apps, but i have a problem changing the layout
, every time when i create new android project , it gives me RelativeLayout
instead of LinearLayout
, (well, basically i am following the book instruction, but it doesn't teach me how to change it) I assume there must be a default setting so that i can change the Relativelayout
to LinearLayout
.
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
Convert a layout To convert an existing layout to a constraint layout, follow these steps: Open your layout in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click the layout and click Convert layout to ConstraintLayout.
Convert a view or layoutClick the Design button in the top-right corner of the editor window. In the Component Tree, right-click the view or layout, and then click Convert view.... In the dialog that appears, choose the new type of view or layout, and then click Apply.
Android provides the following ViewGroups or layouts: LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.
In Latest versions of android studio i believe this will be the way to do it.
Go to res-> layout folder and Right Click on it and you get a sidebar asking for what type of file you want to create
Go to -> Edit File Templates option.
You will see this dialog opened. You see both options right there inside Other tab These options are LayoutResourceFile.xml and LayoutResourceFile_vertical.xml
Change the ${ROOT_TAG} to LinearLayout in both of these LayoutResourceFile.xml and LayoutResourceFile_vertical.xml and you will always get LinearLayout as parent for future.
Replace the following tags :
<RelativeLayout
... >
</RelativeLayout>
by those :
<LinearLayout
... >
</LinearLayout>
In addition, you should remove the following from your TextView attributes :
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
because they apply for the relative layout and not the linear layout.
If you are using Eclipse, just: Rigth click, change layout and select the layout of your choice.
Img example:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With