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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With