4

I want my android layout to behave like so: Suppose SomeLayout is a parent of Widget1, Layout2, and Widget2, which are displayed in that order from top to bottom, like so:

|-------SomeLayout-------|
||--------Widget1-------||
||______________________||
||--------Layout2-------||
||______________________||
||--------Widget2-------||
||______________________||
|________________________|

The 3 children have content that is changing. Dynamically as they change, I always want the one with the widest content to wrap to content. I want SomeLayout to wrap around that widest child, and I want all other children to match that widest one. Even if which one is widest changes.

I have been trying for some time to accomplish this using more methods than I can count. All failures. Does anyone know haw to achieve this effect via the android layout XML? I would be willing to use a Java programmatic solution if that's all that anyone can provide, but I would prefer to do it via just the XML.

FireWingLead
  • 439
  • 6
  • 19

2 Answers2

1

Good Question buddy. After started checking I felt this was a good question..

Here is what you want.. Check it out..

Its just simple one.. Apply some layout width to wrap_content and all direct child's width to match_parent..

see the example below.. you can conform it assigining the background color.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- This is Your Some Layout -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- Widget1 -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2431234vb sset " />

        <!-- Your Layout 2 (inner one) -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2431234v " />
        </LinearLayout>

        <!-- Widget2 -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2431234vb sset " />
    </LinearLayout>

</LinearLayout>

Hope It will help you..

jignesh.world
  • 1,396
  • 1
  • 11
  • 26
0

I hope your SomeLayout extends ViewGroup, so I'd try to override onMeasure method in order to measure all the childs in a proper way. For example

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int specWidth = MeasureSpec.getSize(widthMeasureSpec);
    int maxWidth = 0;
    int maxIndex = 0;
    for (int i = 0; i < getChildCount(); i++){
        View child = getChildAt(i);
        final int widthSpec = MeasureSpec.makeMeasureSpec(
                    specWidth, MeasureSpec.UNSPECIFIED);
        final int heightSpec = MeasureSpec.makeMeasureSpec(
                    0, MeasureSpec.UNSPECIFIED);
        child.measure(widthSpec, heightSpec);
        int width = child.getMeasuredWidth();
        if (width > maxWidth){
            maxWidth = width;
            maxIndex = i;
        }
    }
    for (int i = 0; i < getChildCount(); i++){
        if (i != maxIndex){
            View child = getChildAt(i);
            final int widthSpec = MeasureSpec.makeMeasureSpec(
                    maxWidth, MeasureSpec.EXACTLY);
            final int heightSpec = MeasureSpec.makeMeasureSpec(
                    0, MeasureSpec.UNSPECIFIED);
            child.measure(widthSpec, heightSpec);
        }
    }
    setMeasuredDimension(someMeasuredWidth, someMeasuredHeight);
}

So you should define the widest child and measure all the others in order to adjust maximum size. After that you should layout all the childs in onLayout method implementation according measured dimensions.

Hope it will help you

rus1f1kat0R
  • 1,645
  • 1
  • 16
  • 22
  • I come here looking for a method to make all childs layouts of same height and I manage to do so by using your method, however I had to delete the last line `setMeasuredDimension(someMeasuredWidth, someMeasuredHeight);` and I also added in second line `super.onMeasure(widthMeasureSpec, heightMeasureSpec);` – Epaminondas Oct 19 '14 at 19:54