Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Android layout from Relative to Linear

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>
like image 336
George Lim Avatar asked Aug 08 '12 06:08

George Lim


People also ask

How do I change to linear layout?

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" .

How do I change a relative layout to constraint layout?

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.

How do I change the layout of my android phone?

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.

What is difference between relative layout and linear layout in android?

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.


3 Answers

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.

enter image description here

You will see this dialog opened. You see both options right there inside Other tab These options are LayoutResourceFile.xml and LayoutResourceFile_vertical.xml

enter image description here

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.

Change

like image 62
TapanHP Avatar answered Oct 14 '22 09:10

TapanHP


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.

like image 22
Leeeeeeelo Avatar answered Oct 14 '22 10:10

Leeeeeeelo


If you are using Eclipse, just: Rigth click, change layout and select the layout of your choice.

Img example:

enter image description here

like image 24
RainwalkerJake Avatar answered Oct 14 '22 09:10

RainwalkerJake