1

I am quite familiar with RelativeLayout of Android. However, since I have up updated Android Studio(2.3), ConstraintLayout is set as the default layout of all the new projects.

The problem is:

  • I am not able to delete the ConstraintLayout.
  • How can I set it (RelativeLayout) as default so that I don't have to tweak around.

I was able to learn about the tweak around from this answer: How to switch from the default ConstraintLayout to RelativeLayout in Android Studio 2.3.3

Also please provide me any helpful information about ConstraintLayout, ie. why use it over relative layout, tutorial/help text regarding ConstraintLayout so that I can learn it.

Community
  • 1
  • 1
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
  • switch view to code, and change layout.. – miljon Mar 27 '17 at 16:25
  • @Mij It doesn't work. Keeps showing error. Moreover, how do I set RelativeLayout as default layout of any new project? Also changing the code on every layout on every project is a bit annoying. – BlackBeard Mar 27 '17 at 16:28
  • 2
    I agree that forcing Constraint Layout is a bold move from Google. As someone who has been involved since early alphas, I can say it works "most of the time ok". I can say with confidence it *does not* work 100% ok all the time (some layouts and tricks are harder and/or noticeably slower). That being said, I think it *is* the future of Android Layouts (whether we like it or not) and, much like Apple did with AutoLayout back I the day, you'd want to master it, use it and workaround it when it fails. The best way to learn? Google Documentation + Experimentation. It's not hard at all. – Martin Marconcini Mar 27 '17 at 16:45
  • 1
    Take the leap of faith! You will be very happy, ConstraintLayout provides you with a lot more flexibility than what the RelativeLayout has ever given. Sure it has a slight learning curve but I'd say its worth it :) – gaara87 Mar 28 '17 at 00:45
  • @gaara87 can you please point me toward some tutorial/doc other than that of Google's? – BlackBeard Mar 28 '17 at 02:45
  • Do the official developer docs not serve your purpose? – gaara87 Mar 28 '17 at 03:07

3 Answers3

7

To change layout go to your xml code and change your root layout to desired layout, if it shows error, post error message here.

enter image description here

This part is from my other answer: https://stackoverflow.com/a/42779569/3870382

To change defalut layout when creating an Activity, you can modify default template layout file in Android Studio resources, go to your Android Studio folder and go to following folders path to it:

\plugins\android\lib\templates\activities\common\root\res\layout

Edit file simple.xml.ftl and change layout to your choice, notice that some layouts require additional elements (e. g. LinearLayout needs android:orientation), save file and create activity in Android Studio, you should get an Activity with xml layout that you changed.

Mine looks like this (I had 2.2.3 in that time so I have still RelativeLayout)

(You should see your constrainLayout in this file, so change it to relativeLayout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
<#if hasAppBar && appBarLayoutName??>
    xmlns:app="http://schemas.android.com/apk/res-auto"
</#if>
    android:id="@+id/${simpleLayoutName}"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
<#if hasAppBar && appBarLayoutName??>
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/${appBarLayoutName}"
</#if>
    tools:context="${relativePackage}.${activityClass}">

<#if isNewProject!false>
    <TextView
<#if includeCppSupport!false>
        android:id="@+id/sample_text"
</#if>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</#if>
</RelativeLayout>
Community
  • 1
  • 1
miljon
  • 2,611
  • 1
  • 16
  • 19
3

You may have already tried this, but if not. Go to the top menu of your Android Studio with a project open. "Window" tab, Then pull-down to "Store Current Layout as default" and "Restore to default layout". If the default is not what you like, download a layout you would like as default and set it as your default layout.

I pretty much always use RelativeLayout or LinearLayout as well.. Unfortunately I am not versed on ConstraintLayout.

Hope this helps! :)

  • Did now knew that. It was helpful indeed :) – BlackBeard Mar 27 '17 at 17:04
  • 1
    That doesn't change the layout for future projects.. After doing this and creating a new project, Android will still download the ConstraintLayout from the SDK Tools and set the new project layout as default. I think Window->Layout is for the layout of the IDE (How each tool and windows are placed) – NaturalBornCamper Aug 09 '17 at 10:16
  • I agree with @NaturalBornCamper , as in any other software window->Layout is an option to set the software interface panels and "Store Current Layout as default" is an option to set which panels (project explorer , build, event log ...etc) are opened in the IDE but it has nothing to do with project activities layout. – sh.e.salh Oct 05 '18 at 02:36
1

I am answering this for android studio 2.3.1. One of the easiest way to set RelativeLayout as main layout instead of the constraint layout is going to text mode and editing the xml file as followed

Change this line

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

To

<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

And do check your ending tag changes to this

</android.widget.RelativeLayout>

Also go ahead and delete this line if its being shown in grey.

xmlns:app="http://schemas.android.com/apk/res-auto"

Hope it solves the issue.

Hargun Singh
  • 544
  • 7
  • 19