Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to create listview inside dialog?

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?

like image 658
UMAR-MOBITSOLUTIONS Avatar asked May 20 '10 13:05

UMAR-MOBITSOLUTIONS


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.

How do I show a list in alerts?

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.

Which method is used to set the list in alert dialog box?

setIcon() method is use to set the icon on Alert dialog box.


2 Answers

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();         }     }); 
like image 130
moonlightcheese Avatar answered Sep 22 '22 21:09

moonlightcheese


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> 
like image 26
gvaish Avatar answered Sep 18 '22 21:09

gvaish