14

In my app the users are able to select articles to download using different criteria. One of them is year and month. For this I would like an AlertDialog with a list of years. If the user then clicks on a year, the list will expand and show january, february etc.

I know how to make an expandable listview using a SimpleExpandableListAdapter but that is not what I want. Since the other criteria (eg. category) are also list AlertDialogs, I want something that is similar in look and feel.

Is it possible to accomplish such an expandable list AlertDialog?

SOLUTION

This is what I ended up with based on CommonsWare's solution:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");

ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);

builder.setView(myList);
AlertDialog dialog = builder.create();
dialog.show();

Only problem remaining: how do I implement the onClick listener for the AlertDialog? Normally I would do it in the setItems() method, but am not using setItems.

I added myList.setOnItemClickListener after myList.setAdapter() but it is ignored. Nothing happens when I click an item:

myList.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
        try {
            Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
        }
        catch(Exception e) {
            System.out.println("something wrong here    ");
        }
    }
});

Solution to click problem:

The solution was quite simple. Since it is an expandable list, item clicks are captured by the list itself to open the child elements. Thus, the event handler is never called.

Instead you have to implement OnChildClickListener() that - as the name suggests - listens to child clicks!

marlar
  • 3,858
  • 6
  • 37
  • 60

3 Answers3

10

Use setView() on AlertDialog.Builder, passing in an ExpandableListView that you inflate or create in Java code and have set your adapter on.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have worked a bit on this and I get the basic idea, but how do I create the listview? Normally I would create an activity and inflate it from xml, but in this case I am not going to show the listview except in the dialog. So I somehow need to create the expandable listview inside my main activity and pass it on using setView. How do I do that? Thanks. – marlar Jul 14 '11 at 10:17
  • @marlar: Either inflate a layout XML resource that contains an `ExpandableListView` or create one via Java (e.g., new ExpandableListView(this)`). – CommonsWare Jul 14 '11 at 10:18
  • I got it working! Only problem now, how do I implement onClick when I am not using builder.setItems() ? Nothing happens when I click the items, the dialog doesn't even close. – marlar Jul 14 '11 at 11:22
  • @marlar: Call `setOnItemClickListener()` on the `ExpandableListView` to find out about clicks. – CommonsWare Jul 14 '11 at 12:29
  • I tried this, but apparently it is ignored. Nothing happens when I click the items. See the code in my edited post. – marlar Jul 14 '11 at 12:47
  • @marlar: I have no further suggestions, other than to switch to a dialog-themed activity instead. – CommonsWare Jul 14 '11 at 12:49
  • Ok. I will accept the answer and ask another question if I don't solve this click issue myself. Thanks a lot for your help. – marlar Jul 14 '11 at 12:54
  • 2
    The trick was simply to implement OnChildClickListener() instead of OnItemClickListener() ! – marlar Jul 15 '11 at 22:51
  • Hi @CommonsWare I'm trying to implement expandableListView with checkbox at childView. the check event for checkbox is not working can you please help. My Question https://stackoverflow.com/questions/48547714 – Inzimam Tariq IT Jan 31 '18 at 20:41
1

Well, to use the listviews you have to extend the appropriate list-activity, in your case ExpandableListActivity. You wont be able to find a ExpandableListDialog to extend.

I suppose you might be able to implement it in the activity which calls the dialog, and pass on the listview to the dialog as a reference, and manually add it to your layout in the dialog. Im not sure if this will work, but its worth a shot :D

JustDanyul
  • 13,813
  • 7
  • 53
  • 71
0

you can even make android:theme="Theme.Dialog" in the Manifest file on that particular Activity.

TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
Bharat Chhatre
  • 305
  • 3
  • 6