How to set View width (height) in percentage of parent View in Android

Published by Igor Khrupin on

Example:
I want to set child LinearLayout height = 50% of parrent LinearLayout in Android. Like in HTML.

To set height in percents I do some trick with android:layout_weight and android:weightSum

Here the screenshot:

To do this you need do somethind like this. Look at code below:

<?xml version="1.0" encoding="UTF-8"?>
<linearlayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightsum="100">
        <linearlayout 
            android:layout_weight="50" 
            android:layout_width="match_parent" 
            android:id="@+id/linearLayout1" 
            android:layout_height="wrap_content" 
            android:orientation="vertical" />
</linearlayout>

1 Comment

Mohamed Amine Zghal · 21 January, 2017 at 12:40

How to set margin with % of linearlayout height?

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.