9

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>
sunil
  • 6,444
  • 1
  • 32
  • 44
George Lim
  • 145
  • 1
  • 2
  • 8

6 Answers6

19

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.

Leeeeeeelo
  • 4,333
  • 3
  • 34
  • 44
7

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
sunil
  • 6,444
  • 1
  • 32
  • 44
  • These actions can **NOT** change default **root** layout . – SAYE Apr 28 '18 at 20:42
  • @sam I was mentioning the steps how to create a new layout xml file with different root element, in eclipse. This answer was accepted by the thread initiator at that time. May be he might be looking for a similar solution. – sunil Jun 06 '18 at 09:45
  • yep ! but right now in `android studio` you can't do . and because in google search this question is first i want help – SAYE Jun 06 '18 at 15:12
  • @sam In android studio, you can just change the parent layout by typing in the starting tag. The closing tag will automatically changed by Android Studio. Or are you looking for something else? – sunil Jun 07 '18 at 05:35
  • i mean change default Root `Layout` when create a new layout . otherwise default root element for all layouts is `constraintlayout` and you can change it – SAYE Jun 07 '18 at 09:10
  • @sam I see... I got your point. Please see this SO thread https://stackoverflow.com/questions/42261712/how-to-switch-from-the-default-constraintlayout-to-relativelayout-in-android-stu/42261842. Please see the third answer from `nexus5geozhi q` or `Amir Poursadegh`. – sunil Jun 12 '18 at 09:24
7

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

Img example:

enter image description here

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • All the other answers were also correct, and it is true that an easier way is to just change the text in the xml directly, but this is what I was looking for - how to do it the "eclipse" way. Thanks. – Noam Gal Oct 11 '13 at 10:33
  • 1
    @RainwalkerJake do you know if there is a similar feature in Android Studio? The default seems to always be Relative Layout and I never realise until it's to late, and hate there is no easy way to just change to Linear like there is in Eclipse. – WORMSS Mar 04 '15 at 11:12
4

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

AAnkit
  • 27,299
  • 12
  • 60
  • 71
2

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

TapanHP
  • 5,969
  • 6
  • 37
  • 66
0

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.

Akshay
  • 2,506
  • 4
  • 34
  • 55