0

I have a linear layout with two listviews and two dead buttons. How to divide the space equally between the list views. I got some suggestions like setting the height to 0dip but it didn't work. The graphical layout in eclipse shows a correct layout but when I add different number of elements to the lists they expand to different height. Here is the xml.

<?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" 
    android:weightSum="2">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="Installed engines" >
    </Button>

    <ListView
        android:id="@+id/primary"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" >
    </ListView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="Active engines" >
    </Button>

    <ListView
        android:id="@+id/secondary"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

thanks

Since no one believes me here is an image of what I am getting. Just the links since I don't have enough reputation to post pics :( ! run ! eclipse

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

After all this may not be xml issue based on this answer Linear Layout and weight in Android

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This is correct for static XML layouts. If you're adding Views dynamically at runtime, you'll need to use addView with layout parameters like addView(button, new LinearLayout.LayoutParams(0, height, 1)); This is true even if you're inflating layouts with the correct width and weight values. – Nuthatch Sep 3 '11 at 21:41

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I do use the layout inflater to add my view to a tab. Maybe that is the problem .. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Here is sample code that reproduces the problem when I use an inflator.

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ParticipantsPanel p = new ParticipantsPanel(this);
        setContentView(p);
    }
}

class ParticipantsPanel extends LinearLayout {
    public ParticipantsPanel(Context ctx) {
        super(ctx);

        LayoutInflater li = LayoutInflater.from(ctx);
        View myView = li.inflate(R.layout.participants, null);
        final ListView primary = (ListView) myView.findViewById(R.id.primary);
        final ListView secondary = (ListView) myView.findViewById(R.id.secondary);
        final ArrayAdapter<String> primaryA = 
            new ArrayAdapter<String>(ctx,R.layout.lvtext);
        final ArrayAdapter<String> secondaryA = 
            new ArrayAdapter<String>(ctx,R.layout.lvtext);

        primaryA.add("hello1");
        primaryA.add("hello2");
        primaryA.add("hello3");
        primaryA.add("hello4");
        primaryA.add("hello5");
        primaryA.add("hello6");
        primaryA.add("hello7");
        primaryA.add("hello8");

        secondaryA.add("select1");
        secondaryA.add("select2");

        primary.setAdapter(primaryA);
        secondary.setAdapter(secondaryA);

        addView(myView);
    }
}

xxxxxxxxxxxxxxxxxxxxxxx

Yet another update. I think the problem is as Nuthatch ( the guy I referred to from an old post) pointed out : addView() just messes up whatever layout you have in static xml, which is a valuable lesson imo. When I move the code in the ParticipantsPanel to the main activity, and set the view directly without out adding it to another linearlayout, it works as expected. I appreciate an explanation as to why that is the case, and how I may still put my code in a class and have desired behavior.

Community
  • 1
  • 1
danny
  • 1,101
  • 1
  • 12
  • 34
  • That's because you should be setting `layout_height="0dp"` – K-ballo May 19 '12 at 17:58
  • I mentioned that in my post but it didn't work. Note that it works as expected with equal number of elements in both. But not otherwise. – danny May 19 '12 at 18:01
  • Yes, I read it. That's why that was a comment instead of an answer, despite being the answer. – K-ballo May 19 '12 at 18:02
  • Are you saying it is the answer ? I would appreciate it if you could modify the xml so that it works as intended. – danny May 19 '12 at 18:06
  • Setting the `layout_height` for the two `ListViews` to `0dp` as K-ballo already said will make the lists have the same height(even with different numbers of rows for the two `ListViews`). How exactly this doesn't work for you? – user May 19 '12 at 20:14
  • When an element in the 'primary' list is clicked , I add it to the secondary list. This is a "multi-selection" method that allows for elements to be selected twice or more. Even when it is first displayed (differnt number of rows in the two lists), they have different sizes. – danny May 19 '12 at 23:14

2 Answers2

1

That code you posted is not a good example. The problem of the two ListViews that don't take equal space when you use the LayoutInflater is because you inflate the layout file without supplying a parent ViewGroup from which to take its LayoutParams.

If you use:

addView(myView, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));

or modify the R.layout.participants and replace the parent LinearLayout with a merge tag and also setting:

        this.setOrientation(LinearLayout.VERTICAL);
        this.setWeightSum(2.0f);
        li.inflate(R.layout.participants, this, true);

for the ParticipantsPanel,

then all should be alright.

user
  • 86,916
  • 18
  • 197
  • 190
  • This is exactly what the guy ,Nutatch, warned i.e addView messes up layout static layout parameters. Thanks for the explanation as to why that is the case. – danny May 20 '12 at 14:42
  • This worked for me if (view == null) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); view = inflater.inflate(R.layout.single_list_item, parent, false); } – Pranita Patil Nov 26 '13 at 09:42
0

for vertical orientation , set the height of the weighted views to 0px (or 0dp , or whatver, 0 is a 0 in all units on android) . for horizontal orientation do the same but for width.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • That is exactly what I did but it didn't work. For static elements like buttons it does work. I have two list views with different content and they somehow readjust themselves to differnt sizes. I have updated the xml file. – danny May 19 '12 at 23:08
  • I have uploaded pictures now. – danny May 19 '12 at 23:26
  • well that is weird. i knew that the visual editor still can't be trusted . wonder if on a real device you would have the same results. anyway, you know , you can use a workaround by putting each listview in a frameLayout which will have the weights , while the listview will simply expand as much as it can (match-parent) . this should restrict the listViews so that they will act nicely. – android developer May 20 '12 at 06:01
  • I tried FrameLayout now & it didn't work. I am fairly convinced the issue is with my use of inflator to bring up the view, as the guy in my edited post said. It simply forgets whatever layout I set unless I use code like setLayoutParams(....). – danny May 20 '12 at 08:24
  • well if you use "setLayoutParams" for any of the linearLayout's children (meaning the 2 buttons and the 2 listViews/FrameLayouts) , you will have to keep the same parameters values so that it won't change . you can use "getLayoutParams" , change what you need , and call "setLayoutParams" . in any case , using those on the listView's children should not do anything that causes the problem. if you still have problems , please post a minimal test project . you don't have to put your original code. – android developer May 20 '12 at 09:31
  • I have posted a minimal code that reproduces my problem. With equal number of elements in both lists I get equal space division but not otherwise.. – danny May 20 '12 at 13:50
  • 1
    by giving different background colors to each of the views (including the root) , i've found out that your root view (called "myView" ) doesn't take the whole space that it can , so the only thing missing is to give it layoutParams : myView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); now it works perfectly . for future cases, i suggest that you set a background for each of the views in order to learn what went wrong. – android developer May 20 '12 at 19:39