androidandroid-layoutandroid-constraintlayoutconstraintlayout-guideline

How to set height of a view in percent using ConstraintLayout


Currently I'm learning ConstraintLayout

below is my code that i have tried so far

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="#000000"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.PhaseListActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.8"
                android:background="@color/colorAccent">

                <VideoView
                    android:id="@+id/myVideoView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="visible" />

                <TextSwitcher
                    android:id="@+id/tvAnimText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:fontFamily="@font/knowledge_bold"
                    android:textColor="@color/greenColor"
                    android:textSize="@dimen/_100ssp"
                    android:textStyle="bold"
                    tools:text="1" />

            </FrameLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


    </androidx.core.widget.NestedScrollView>


    <LinearLayout
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="@color/greenColor"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Fixed Bottombar"
            android:textColor="#ff00"
            android:textStyle="bold" />
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

I want don't want use nested LinearLayout inside ConstraintLayout

Also in above layout my NestedScrollView is not scrolling

I want to create this type of screen using ConstraintLayout

My expected OUTPUT

enter image description here

I have tried to set Guideline but it didn't work for me, because i don't know how to use Guideline in above layout

Can anybody help me create this screen only using ConstraintLayout

If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.


Solution

  • So, after looking at your xml file, I've come up with solution to get rid of nested LinearLayout hierarchy in below solution:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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"
        android:background="#000000"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toTopOf="@id/tv_bottom_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <VideoView
                android:id="@+id/myVideoView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:visibility="visible"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toTopOf="@id/tv1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="percent"
                app:layout_constraintHeight_percent="0.85"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed" />
    
            <TextSwitcher
                android:id="@+id/tvAnimText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:fontFamily="@font/knowledge_bold"
                android:textColor="@color/greenColor"
                android:textSize="@dimen/_100ssp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@id/myVideoView"
                app:layout_constraintEnd_toEndOf="@id/myVideoView"
                app:layout_constraintStart_toStartOf="@id/myVideoView"
                app:layout_constraintTop_toTopOf="@id/myVideoView"
                tools:text="1" />
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:text="Scrollbale view below my videoView"
                android:textColor="#ff00"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@id/tv2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/myVideoView" />
    
            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:text="Scrollbale view below my videoView"
                android:textColor="#ff00"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@id/tv3"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tv1" />
    
            <TextView
                android:id="@+id/tv3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:text="Scrollbale view below my videoView"
                android:textColor="#ff00"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tv2" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
    
    <TextView
        android:id="@+id/tv_bottom_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="Fixed Bottombar"
        android:textColor="#ff00"
        android:textStyle="bold"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.08"
        app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Let me know if there's any query in comments. For your fixed bottombar TextView, now you can change height percent to whatever you want to change it's height dynamically.