2

Can we change the default Build behavior of Android Studio from Constraint to Relative Layout?

Problem: Sometimes it takes more time to Clean/Rebuild my project. And I never use Constraint Layout, so I want to set the build behavior of Android Studio from Constraint Layout to Relative Layout.

I want every-time I open AS, it should open Relative. Is it possible to achieve?

Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
  • Thanks @Zaid But that's not what I meant. I want to change my default build behavior of AS and your link shows how to switch your default layout to Relative. Please read my question again. – Rohit Sharma Mar 04 '18 at 18:22

3 Answers3

1

Make default layout to Relative for current project

First open your .xml file and change your constraint layout to relative layout. Then go to the top menu of your Android Studio with a project open. Then go to "Window" tab, Then select "Store Current Layout as default". If the default is not what you like, download a layout you would like as default and set it as your default layout.

Make default layout to Relative for all projects

Just modify the template layout file which is present in android studio resources

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout

Now edit this file simple.xml.ftl and change layout to your choice(Note. for making default relative just copy below code in your simple.xml.ftl file), notice that some layouts require additional elements (e. g. LinearLayout needs android:orientation), save file and create activity in Android Studio, it should work.

<?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>
MashukKhan
  • 1,946
  • 1
  • 27
  • 46
1

Keep in mind if you modify the simple.xml, the android studio updater will detect a conflict and prevent applying the update. Keep your copy :)

coltdorsey
  • 93
  • 5
  • Yes. That is very valid point. Thanks! I experienced this while `check for update` and after 3 unsuccessful attempts. :) Only solution that worked was to uninstall AS and download a fresh copy from `https://developer.android.com/studio/` – Rohit Sharma Jun 10 '18 at 02:47
0

Quite Easy - just in 3 steps

  1. Goto C:\Program Files\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout
  2. Backup simple.xml.ftl
  3. Modify simple.xml.ftl to below code and save it

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}">

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Sample"
    android:textSize="16sp" />
</RelativeLayout>

Tried and tested on Windows 10 build:gradle:3.0.1

  • Works for every new activity in the same project and for future projects as well.
  • Note: Need to modify the simple.xml.ftl file with Admin rights
  • For this to work out you don't need to edit layout > New > Edit File Templates... your layoutResourceFile and layoutResourceFile_vertical by removing the ${ROOT_TAG} - as I have already tried but that doesn't work.
Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37