Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine layout_weight with minWidth

I have a layout which I want to split into 2 views using layout_weight (1:2). But, I want the left view to have at least 400dp width.

For example, if the left view gets 420dp width by using weight then leave it, but if it has less than 400dp, than let it be 400dp and give the other view all the rest.

This is the layout I've tried and did not work for me.

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:minWidth="400dp"
            android:background="@android:color/holo_blue_bright"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@android:color/holo_green_light"/>

    </LinearLayout>

Please help, Thanks!

like image 621
Yaniv Avatar asked Aug 13 '15 06:08

Yaniv


1 Answers

I think this is what you need:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <View
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="400dp"
        android:background="@android:color/holo_blue_bright"/>

    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"/>

</LinearLayout>

Basically, let the first layout take the needed space but no less than 400dp. The second one will take all the remaining. As with all the cases when weight is involved, be sure that the needed space (for width) for the 2 children is less than what the parent can offer, otherwise you will have things off screen.

Note: I tried it on phone layout, but 400dp was off screen in portrait so it seemed like the first layout was taking all the space, so please make sure to try it out on a device with more than 400dp in the direction you want your layout span :-)

like image 52
N.T. Avatar answered Oct 01 '22 17:10

N.T.