32

i have created a custom dialog class

public class NewPost extends Dialog
{
// functionality

}

now my requirement is to create listview inside it. i know we can create textboxes,buttons,dropdown list inside it.

but in order to create list view we should inherit our class from listActivity class

what you suggest is it possible or not if yes then how to achieve this using any interface or what?

Cœur
  • 32,421
  • 21
  • 173
  • 232
UMAR-MOBITSOLUTIONS
  • 73,009
  • 94
  • 197
  • 273

6 Answers6

64

this implementation doesn't require you to make any xml layouts. it was written as a case statement in "onCreateDialog" override, but you can adapt if for your purposes very easily:

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

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
final Dialog dialog = builder.create();

dialog.show();

because you are making a dialog with only a ListView, you set the onItemClickListener of the ListView, as there isn't one for the basic dialog class.

modeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch(i) {
                case 0:
                    //do something for first selection
                    break;
                case 1:
                    //do something for second selection
                    break;
            }
            dialog.dismiss();
        }
    });
moonlightcheese
  • 10,354
  • 7
  • 45
  • 69
  • I was hoping to use this for picking a selection from a dialog, but when I added an onItemClickListener I couldn't access the dialog.dismiss() from the onItemClick. I'm sure someone cleverer than me can hook that up but beware that this answer needs more code to be useful. – Jeff Feb 14 '12 at 20:37
  • Just declare the dialog variable as private at the top of your class and then call dialog.dismiss() in the on click event. Worked for me – Nick Feb 29 '12 at 16:35
  • 3
    Jeff - that's because ListView.OnItemClickListener does not give you the dialog parameter in the way that DialogInterface.OnMultiChoiceClickListener does. declaring the dialog as final should take care of this and allow you to access the dialog within your override. hth. – moonlightcheese Mar 01 '12 at 18:00
  • 1
    @moonlightcheese Display is working. And how to get on clicked item ? – Sam Apr 01 '18 at 14:56
51

Yes.

You can always use a ListView inside a Dialog. And you definitely don't necessarily need ListActivity to create ListView.

Code may be something like this:

Dialog dlg = new Dialog(context);
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.my_layout, null, false);
dlg.setContentView(v);
dlg.show();

my_layout.xml:

<ScrollView xmlns:android="blah"
   android:id="xid"
   android:layout_height="h"
   android:layout_width="w">

  <ListView blah blah blah attributes
  />

 </ScrollView>
Gaurav Vaish
  • 9,140
  • 3
  • 35
  • 43
11

You don't really have to extend listActivity in order to use listviews.

Extending listActivity will give you some functionality for free, such as getListView() (if I recall the method name correctly), but that can just as well be done manually with findViewById() just as any other view.

David Hedlund
  • 121,697
  • 28
  • 196
  • 213
  • stuck in same can anyone help http://stackoverflow.com/questions/29446088/how-to-get-spinner-values-in-textview/29487383?noredirect=1#comment47175570_29487383 –  Apr 08 '15 at 13:48
4

The simplest possible way:

    ListView listView = new ListView(this);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"item 1", "item 2", "item 3"}));
    Dialog dialog = new Dialog(this);
    dialog.setContentView(listView);
    dialog.show();
konmik
  • 2,787
  • 16
  • 15
3

You can create a custom dialog with this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:background="@drawable/dialogs">
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        android:paddingTop="10dip">
        <ImageView 
            android:id="@+id/dialog_title_image"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/info"/>
        <TextView 
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="20dp"
            android:layout_centerInParent="true"
            android:text="Title"
            android:layout_toRightOf="@id/dialog_title_image"
            android:textColor="@android:color/black"
            android:textSize="20sp"/>

        <!-- Lista -->
    </RelativeLayout>
    <TextView 
            android:layout_width="fill_parent"
            android:layout_height="3dip"
            android:background="#1e90ff"/>
    <ListView
            android:id="@+id/component_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    <!-- Fine lista -->

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dip"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="bottom|center_horizontal"
        android:paddingBottom="20dip">
        <Button 
            android:id="@+id/positive_button"
            android:layout_alignParentLeft="true"
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:background="#1e90ff"
            android:textColor="@android:color/white"
            android:text="@string/close"/>

    </RelativeLayout>
</LinearLayout>

create a custom layout also for each item if you want:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />


</LinearLayout>

and then use an arraylist to populate the list and set the view:

        //creation and population of the list
        List<Component> my_list = new ArrayList<Component>();
        createComponents();

        //adapter
        array_adapter = new ComponentAdapter(context, R.layout.component,my_list);

        //button to show the dialog
        Button button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                list_dialog = new Dialog(context);
                list_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                list_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                list_dialog.setContentView(R.layout.list_dialog);

                ListView list = (ListView)list_dialog.findViewById(R.id.component_list);
                list.setAdapter(array_adapter);

                Button positiveButton = (Button) list_dialog.findViewById(R.id.positive_button);

                positiveButton.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {

                        list_dialog.dismiss();  
                    }
                });

                list_dialog.show();     
            }
        });


    }

References: http://pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links

Result: enter image description here

SegFault
  • 1,520
  • 2
  • 19
  • 36
0

You can use any layout for alert dialogs. If you want a listview I would do it like here

Community
  • 1
  • 1