Skip to content Skip to sidebar Skip to footer

What Does 'view' Container Do In XML Of Android

In this XML what is advantage of using view?

Here is your layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/coordinatorLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/view"
            android:layout_width="100dp"
            android:layout_height="720dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@color/black_2"/>

        <Button
            android:id="@+id/toggleBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="72dp"
            android:text="this is button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="120dp"
            android:text="this is text view"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="@+id/toggleBtn"
            app:layout_constraintStart_toStartOf="@+id/toggleBtn"
            app:layout_constraintTop_toBottomOf="@+id/toggleBtn" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Image with view

Image with view

So, if you have view, because of the height of ConstraintLayout is 720dp the ConstraintLayout will stretch to maximum because of wrap_content behaviour.

Height of Constraint Layout with View

Height of Constraint Layout

And if you remove view, then the height of ConstraintLayout also changes

Height of Constraint Layout without View

Height of Constraint Layout without  View

It depends on your requirement, you need to have View or not, but currently it has no impact.


Solution 2:

I think in this code, view application is clear:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:tabGravity="fill"
        app:tabMode="fixed" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#c6c4c4" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

When view is in xml output will be:

enter image description here

But without view in xml output will be:

enter image description here


Post a Comment for "What Does 'view' Container Do In XML Of Android"