Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to give 50 % width to each linear layout not hard coded.


<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:removed="@color/black"
android:orientation="vertical"
>


<linearlayout> android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/ll1"
android:layout_alignParentLeft="true"
android:layout_weight="1">

<textview>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:removed="#aa0000"
android:gravity="center_horizontal"
android:text="red"
android:textSize="24sp" />

<textview>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:removed="#00aa00"
android:gravity="center_horizontal"
android:text="green"
android:textSize="24sp" />


<linearlayout> android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_toRightOf="@id/ll1"
android:layout_weight="1"
>

<textview>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:removed="#aa0000"
android:gravity="center_horizontal"
android:text="red"
android:textSize="24sp" />



<textview>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:removed="#00aa00"
android:gravity="center_horizontal"
android:text="green"
android:textSize="24sp" />





What I have tried:

i tried
android:layout_width="0dp"
android:layout_weight="1"


but it is not working
Posted
Updated 26-Jul-16 23:00pm

1 solution

To provide size distribution without hard coding it, in xml ,you need to do following steps

1. Use "android:weightSum" attribute in parent container and "android:layout_weight" in inner view.
2. In inner view when you are using android:layout_weight, you need to provide "android:layout_height='0'" (if you are dividing it vertically) or "android:layout_width='0dp'" (if you are diving it horizontally).
3. android:weightSum in parent should be summation if all layout_weight in inner view.


Example: Below code shows vertically two 50% linear layout design.

HTML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">50%</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:removed="@color/colorAccent">50%
    </LinearLayout>
</LinearLayout>


Let me know if that helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900