Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint layout as 50% height of screen?

Tags:

android

xml

I have a ConstraintLayout in Android Studio, I want this to have the height be 60% of the screen size for whatever device it is running on, I am wondering how I can do this in xml?

Currently, I have:

<androidx.constraintlayout.widget.ConstraintLayout
     android:id="@+id/recycler_view_container"
     android:layout_width="match_parent"
     android:layout_height="372dp" // BAD!! HARDCODED
     android:background="@color/white">

     ...

</androidx.constraintlayout.widget.ConstraintLayout>

How could I do this? As you see, the layout_height is hardcoded right now.

like image 262
testingsah Avatar asked Oct 30 '25 15:10

testingsah


1 Answers

Here is my answer. I think it should clear your requirements.

<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="@color/colorWhite">

<androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorRed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.50">

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

You can change height percentage according to your needs. Thanks.

like image 93
Geek Tanmoy Avatar answered Nov 01 '25 06:11

Geek Tanmoy