Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 960
lstipakov Avatar asked Jun 21 '11 10:06

lstipakov


People also ask

How can I display a list view in an Android alert dialog?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.

What is the difference between alert dialog and dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is alert dialog box and example?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

How do I show custom dialog?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

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>
like image 101
Sunil Kumar Sahoo Avatar answered Oct 13 '22 00:10

Sunil Kumar Sahoo


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

like image 23
Ramakrishna Avatar answered Oct 13 '22 00:10

Ramakrishna