lstipakov
lstipakov

Reputation: 3238

Dialog with list view and message

I need to create dialog with both ListView and message, however according to http://code.google.com/p/android/issues/detail?id=10948 it is not possible with standard AlertDialog. So I've decided to create custom view with text and listview, and attach it to dialog.

However, my list view is drawn empty. Here is java code:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Hello, title!");

    LayoutInflater factory = LayoutInflater.from(this);
    View content = factory.inflate(R.layout.dialog, null);

    ListView lv = (ListView) content.findViewById(R.id.list);
    lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice, ITEMS));
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    builder.setView(content).setPositiveButton("OK", this).setNegativeButton("Cancel", this);

    AlertDialog alert = builder.create();
    alert.show();

Also I have:

    final String[] ITEMS = new String[] { "a", "b", "c" };

and here is dialog layout:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
    ></ListView>

</LinearLayout>

Here is result: dialog_with_empty_list_view

Any help is greatly appreciated. Thanks!

Upvotes: 11

Views: 7980

Answers (2)

Ramakrishna
Ramakrishna

Reputation: 4086

set orientation is vertical like

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

     <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Hello, text!" />

     <ListView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/list"
     ></ListView>

     </LinearLayout>

in your layout

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

You are missing android:orientation="vertical" in linearlayout.

Your xml will be

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello, text!" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"

    ></ListView>

</LinearLayout>

Upvotes: 4

Related Questions