How to bring back RelativeLayout? - android

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.

To change layout go to your xml code and change your root layout to desired layout, if it shows error, post error message 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>

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! :)

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.

Related

AndroidStudio ConstrainLayout not working

I am beginner in android studio and having headache with this if someone could help me am i missing something?.... I have problem with Constraint layout, the problem is i put some element(ImageView, Button it does not mater) in layout and constraint it to parent but when I run it in emulator it just is not showing. also it is not showing me those buttons under elements connect BaseLine and remove Constrains for that element.
"screenShot":
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="343dp"
android:layout_height="118dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_c`enter code here`onstraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/backgrounds/scenic[7]" />
</android.support.constraint.ConstraintLayout>
when i run it in emulator it just is not showing
The problem is here :
tools:srcCompat="#tools:sample/backgrounds/scenic[7]"
You need to understand what tools is, so I recommend to you to read a little bit the documentation.
Just a note of it :
It's a namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Instead you have to use to show it on the emulator.
app:srcCompat="#drawable/your_drawable"
Note: remember to add : xmlns:app="http://schemas.android.com/apk/res-auto"
I found the solution.it's difrent in new Android Studio 3.4, You have to Right click on element and option Show Base Line is there now.

Want Relative Layout from the beginning

Novice in Android Studio. Recently started practising basic coding. I want to have relative layout or linear layout from the beginning. Tried but could not make it possible. Could not find any option provided to select the layout theme. Automatically constraint layout getting launched. Please guide..
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 (optionally) go ahead and delete this line if it's being shown in grey:
xmlns:app="http://schemas.android.com/apk/res-auto"
And also this is an optional change to make in project,
If the constraint layout is not needed in the project remove the following dependency from build.gradle by deleting this line and then doing gradle sync:
compile 'com.android.support.constraint:constraint-layout:......'

Drag/Drop components are not visible in preview in android studio

I am a beginner in android development. I have created a android project. But in the preview section, I am not able to see the components. But components are visible when i run the same app on emulator i am able to see the components. I am missing some setting here? XML is given below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've had have also this problem and solved it by only selecting another "Theme" cause
the default theme appTheme doesn't show added items.
If you i.e. select Transulcent, any item becomes visible ;-)
cheers
Oliwan
Maybe sound a weird answer, but self-tested this.
Change the XML file's (maybe activity_x.xml) layout's root tag android.support.constraint.ConstraintLayout to LinearLayout, then preview will start working again (as happened in my case). [If not, then perform force refresh (R)]
Then switch back to old one, I mean: Change the XML file's (maybe activity_x.xml) layout's root tag LinearLayout to android.support.constraint.ConstraintLayout. And force a refresh.
This does seem to be a bug in Android Studio. When it showed up for me, I fixed it by making a trivial change in the app build.gradle (change version code), do a gradle sync, then change back and sync again.
After that, the design view worked again.

layout_width is missed after using <android.support.v4.widget.SlidingPaneLayout> tag

I'm new to Android development and trying to learn it by the book Android for Absolute Beginners
One of the chapters is about android U.I Design and i'm learning about different android layouts in this chapter .
My problem starts after adding <android.support.v4.widget.SlidingPaneLayout > Tag to my code in order to use android SlidindPaneLayout .
Before Adding this Tag I could use android:layout_width or android:layout_height and ... attribute in all of objects . But now it can be only used inside my sliding layout tag
Here is a little simplified version of my code which as you can see android:layout_width is not used inside Linear Layout because Eclipse doesn't accept it ;)
for more detail look at the link below Please ;)
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="hamid.traingin.hellouniverse.MainActivity"
android:id="#+id/spLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:background="#drawable/milkyway1">
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
Here is a picture of my problem in Eclipse IDE:
The xml code is valid, you can and should add the layout_width and layout_height to you LinearLayout without raising a compiler error. I'm not too sure why Eclipse does not allow you to do so, but Android Studio does:

xml file vs xml layout

I created two layout files.
Android XML Layout File which created this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and an Android XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
From the looks of it they are both the same. When to use one over the other since they are both creating the same xml?
Yeah they are prograticaly the same, there isn't different type of xml. What matters, is the location of the xml (layout, values, ...).
In eclipse, there maybe is a small difference in the IDE view, i'm not sure that you have the Graphical Layout view if you create a Android XML file instead of a layout file (it's a detail because, you still can add this view ;) )
But results apk will be identical.
The reason why you would use an Android XML, is if you wanted to use the Layout Tool (the GUI layout editor). The Eclipse IDE will react to this file differently.
But, the code is the same, so the APK you create will also be the same - it doesn't matter in your final output.
I always use Android XML files, so they integrate into the ADT tools better.
the only difference as far as I can see is that in xml you can't use both "android:layout-width:" and "android:layout-height:" attributes which makes xml for example suitable for the use of menu and item bar and other uses.

Categories

Resources