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?
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.
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.
setIcon() method is use to set the icon on Alert dialog box.
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(); } });
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>
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