2

I want to create an activity with a ListView on the left and a TextView on the right, side by side. I write the following xml, but the ListView occupies the entire page and it don't worry about the wrap_content. Why? How can I resolve it?

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

    <ListView 
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

EDIT: my onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lv = (ListView) findViewById(R.id.lv);
    String[] values = new String[] { "Test1", "Test2", "Test3", "Test4" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.left, R.id.tv1, values);
    lv.setAdapter(adapter); 
}
gaiapac
  • 31
  • 3

2 Answers2

0

From what you described wrap_content seems to be stretching right across the screen, so let's set a limit where the ListView and TextView share the screen 50/50:

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

    <ListView 
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="200dp"
        android:layout_weight="1" />

</LinearLayout>
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Are you passing this layout to `setContentView()`? Post your `onCreate()` method by clicking "edit" in the lower left corner of your question. – Sam Nov 16 '12 at 22:25
  • Wow, ListViews really don't like sharing. If you change my code to two ListViews they split the screen like they should... Anyway I added the `minWidth` attribute, maybe this will do you want? – Sam Nov 16 '12 at 22:57
0

Edit: try this instead

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/tv" >

    </ListView>

</RelativeLayout>
mango
  • 5,577
  • 4
  • 29
  • 41
  • it works but I don't know which is the right value, I need wrap_content – gaiapac Nov 16 '12 at 22:16
  • what do you mean by the "right value"? `dp` will scale according to screen size. If you need it to coincide with your individual rows, then you need to size your row.xml or row code appropriately and could probably rely on "wrap_content". It's just that the preview wont show it cause it doesn't know what's in there. – mango Nov 16 '12 at 22:19
  • means that the text in the rows of listview has an unknown length and there is the risk of setting a value too low that breaks the line or too high with a lot of useless space – gaiapac Nov 16 '12 at 22:25