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:
Any help is greatly appreciated. Thanks!
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.
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 .
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.
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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With