Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it advisable to use LinearLayout inside ConstraintLayout in Android?

I am new to ConstraintLayout in Android and newbie to Android too. I have a question. Is it advisable to use LinearLayout inside ConstraintLayout? For example:

<?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"
    app:srcCompat="@drawable/landing_screen"
    android:layout_height="match_parent"
    tools:context="com.braigolabs.braigo.landing.LandingActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/landing_screen"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:layout_constraintHorizontal_bias="1.0"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="51dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="66dp"
            android:layout_marginStart="66dp"
            android:gravity="center"
            android:text="@string/login_welcome_braigolabs"                android:textAppearance="@style/TextAppearance.AppCompat.Large"
            tools:layout_editor_absoluteX="93dp"
            tools:layout_editor_absoluteY="403dp"/>

        <Button
            android:id="@+id/login_button"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="2dp"
            android:text="@string/login_login_button_title"
            android:textAllCaps="false"
            tools:layout_editor_absoluteX="116dp"
            tools:layout_editor_absoluteY="543dp"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

Also curious to know how popular is the ConstraintLayout among the developers?

like image 477
TechBee Avatar asked Aug 11 '17 02:08

TechBee


People also ask

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

Is ConstraintLayout faster than LinearLayout?

Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can't say about Constraint Layout. More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

What is constraintlayout in Android?

ConstraintLayout is similar to that of other View Groups which we have seen in Android such as RelativeLayout, LinearLayout, and many more. In this article, we will take a look at using ConstraintLayout in Android. This is used to give a unique id to the layout.

What is the difference between constraintlayout and linear layout?

With the help of ConstraintLayout, we can position our UI components in any sort of order whether it may be horizontal or vertical. But in the case of Linear Layout, we can only arrange our UI components either in a horizontal or in a vertical manner.

What is linearlayout in Android Studio?

LinearLayout is the most basic layout in android studio, that aligns all the children sequentially either in a horizontal manner or a vertical manner by specifying the android:orientation attribute.

What is the difference between constraints layout and grid layout in Android?

In Grid Layout, the UI which is actually seen in the Design editor of Android Studio will be the same as that we will get to see in the app, but in the case of Constraint layout if the UI component is not Constrained then the UI will not look same as that of in design editor.


2 Answers

Is it advisable to use LinearLayout inside ConstraintLayout?

No... usually.

In general, the idea behind ConstraintLayout is that it allows you to position all of your children without having to nest any other ViewGroups inside the ConstraintLayout. As such, I would say that it is not advisable.

However, there are some things that a LinearLayout can do that a ConstraintLayout can't (mostly revolving around weighted spacing of views), and so if you need these particular corner cases in your layout, you won't have any option other than falling back to a LinearLayout.

how popular is the ConstraintLayout among the developers?

ConstraintLayout is relatively new, but it is quite powerful and certainly something that you ought to familiarize yourself with. It won't always be the perfect tool for the job at hand, but it will often allow you to easily create layouts you would otherwise spend hours on.

I can't speak to widespread adoption statistics, but I can say that I've seen tons of questions on this site about the correct usage of ConstraintLayout, so clearly devs around the world are starting to work with it.

like image 77
Ben P. Avatar answered Oct 14 '22 00:10

Ben P.


As of the 2.0.0-alpha5 release of the constraintlayout library it's now possible to declare a Flow virtual layout element within your ConstraintLayout which (as the name suggests) determines how selected items are to flow (e.g. vertically, horizontally) within the ConstraintLayout. So it's no longer necessary to declare a LinearLayout within your ConstraintLayout.

For example, if you wanted items within your ConstraintLayout to flow vertically you'd do so like this:

<androidx.constraintlayout.widget.ConstraintLayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <TextView         android:id="@+id/textView1"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:text="I am the first TextView" />      <TextView         android:id="@+id/textView2"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:text="I am the second TextView" />      <TextView         android:id="@+id/textView3"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:text="I am the third TextView" />      <androidx.constraintlayout.helper.widget.Flow         android:layout_width="0dp"         android:layout_height="0dp"         android:orientation="vertical"         app:constraint_referenced_ids="textView1,textView2,textView3"         app:flow_horizontalAlign="start"         app:flow_verticalGap="8dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout> 

You can play around with the app:flow_ attributes in the Flow element to achieve different flow behaviour. For more information about the Flow element, refer to the release notes here. For an example, see here.

like image 30
Adil Hussain Avatar answered Oct 13 '22 23:10

Adil Hussain