George Lim
George Lim

Reputation: 145

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>

Upvotes: 9

Views: 59292

Answers (6)

TapanHP
TapanHP

Reputation: 6181

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

Upvotes: 2

RainwalkerJake
RainwalkerJake

Reputation: 93

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

Img example:

enter image description here

Upvotes: 7

sunil
sunil

Reputation: 6614

To change the Parent of your layout XML file follow these steps:

  • Right click your layout folder in res folder from project folder in eclipse. (<project-folder/res/layout>)
  • Choose option New -> Other.... Refer picture: select layout folder
  • Choose Android Layout XML file option from here. enter image description here
  • Press Next and select LinearLayout as Root Element and give File Name. Press Finishenter image description here
  • Now your Layout XML has the root element as LinearLayout

Upvotes: 7

Leeeeeeelo
Leeeeeeelo

Reputation: 4383

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.

Upvotes: 19

Akshay
Akshay

Reputation: 2534

When you create new XML file at that time you can assign layout to it.But when you create new project at that time you can not change layout you have to change it after creation of project.So go to .xml tab in XML file there you can edit layout. Hope this helps.

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

You just need to change this tag <RelativeLayout ...> with <LinearLayout ...> , and don't forget to close it with same Layout

Upvotes: 4

Related Questions