Spinners in AlertDialog - android

I have an AlertDialog with two spinner in its layout, if I don't set up an adapter for those spinners my layout its inflating correctly. (https://goo.gl/boniIK)
But once I attach an adapter to those spinners there's a huge white space below my positive and negative buttons. (https://goo.gl/3aJOlN)
Does anyone has an idea why it's this white space appearing?
Thanks in advance.
EDIT
Here is my sort_filter_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/dialog_content_padding_top"
android:paddingBottom="#dimen/dialog_content_padding_bottom"
app:columnCount="4"
app:orientation="horizontal"
android:background="#color/schoolhub_blue">
<Space
android:layout_width="#dimen/dialog_content_space"
app:layout_gravity="fill_vertical"
app:layout_rowSpan="2"/>
<TextView
style="#style/TextView"
android:layout_width="#dimen/none"
android:layout_height="#dimen/dialog_content_row_height"
app:layout_columnWeight="1"
android:text="#string/sort"/>
<Spinner
style="#style/Spinner"
android:id="#+id/spinner_sort"
android:layout_width="#dimen/none"
android:layout_height="#dimen/dialog_content_row_height"
app:layout_columnWeight="1"/>
<Space
android:layout_width="#dimen/dialog_content_space"
app:layout_gravity="fill_vertical"
app:layout_rowSpan="2"/>
<TextView
style="#style/TextView"
android:layout_width="#dimen/none"
android:layout_height="#dimen/dialog_content_row_height"
app:layout_columnWeight="1"
android:text="#string/filter"/>
<Spinner
android:id="#+id/spinner_filter"
android:layout_width="#dimen/none"
android:layout_height="#dimen/dialog_content_row_height"
app:layout_columnWeight="1"/>
<include
layout="#layout/divider"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginBottom="#dimen/divider_margin"
android:layout_marginTop="#dimen/divider_margin"
app:layout_columnSpan="4"/>
</android.support.v7.widget.GridLayout>
And I'm building my AlertDialog in a DialogFragment onCreateDialog method
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
View view = View.inflate(getActivity(), R.layout.sort_filter_dialog, null);
// Set up for sort spinner.
ArrayAdapter<String> sortAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, sortStringArray);
sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sortSpinner = (Spinner) view.findViewById(R.id.spinner_sort);
sortSpinner.setAdapter(sortAdapter);
// Set up for filter spinner.
ArrayAdapter<String> filterAdapter = new ArrayAdapter<>(getActivity(), R.layout.spinner_dropdown_item_selected, filterStringArray);
filterAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
Spinner filterSpinner = (Spinner) view.findViewById(R.id.spinner_filter);
filterSpinner.setAdapter(filterAdapter);
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogStyle);
dialog.setTitle(getString(R.string.sort_filter_title));
dialog.setView(view);
return dialog.create();
}

Without posting your code it's hard to guess but I suspect the issue is the way you are inflating the layout. In most cases when you are using an adapter, inflate it like this:
view = inflater.inflate(R.layout.my_layout, parent, false);

Related

Need transparent background like this [duplicate]

This question already has answers here:
Dialog with transparent background in Android
(25 answers)
Closed 6 years ago.
Need to make a layout like this.
Below is an alert box which has a transparent background containing spinner and a button below.
i ve tried this as far.
this is the layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#fff"
android:paddingBottom="50dp">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/selectCity_xspn"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="6dp"
android:layout_marginTop="75dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/selectCity_xspn"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp"
android:id="#+id/selectCtgry_xspn" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginTop="300dp"
android:layout_alignParentEnd="true" />
This is the java class where i ve used the transparent option at many places but it doesnt work.
accessing the function at the bottom in the following button.
customdialog_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onCreateDialog2().show();
}
});
public Dialog onCreateDialog2() {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(Registeration.this);
View dialog_view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
//dialog_view.setBackgroundResource(android.R.color.transparent);
builder.setView(dialog_view);
selectCity_spn=(Spinner)dialog_view.findViewById(R.id.selectCity_xspn);
selectCtgry_spn=(Spinner)dialog_view.findViewById(R.id.selectCtgry_xspn);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCity_spn, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCity_spn != null;
selectCity_spn.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCtgry_spn, android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCtgry_spn != null;
selectCtgry_spn.setAdapter(adapter2);
View v = getWindow().getDecorView();
v.setBackgroundResource(android.R.color.transparent);
return builder.create();
}
This will do
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
Try this:-
Dialog dialog1 = new Dialog(Main3Activity.this);
dialog1.setCancelable(true);
dialog1.setContentView(view1);
dialog1.show();
you can set background transparent of any layout or any view or any component by adding this code in XML
android:background="#android:color/transparent"

Custom Dialog that allows me to add to its list view and return the final list

I have a custom dialog with a editview (where user puts an email address) and 'add' imageview which when clicked should add the email to the listview and a list view which shows the list of email addresses.
I have 2 issues. Im not sure how add to the listview and update it on ui and secondly i cant seem to get my dialog to open at all. Anybody know the best way to do this?
Custom dialog xml
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:id='#+id/background'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:orientation='vertical'
android:padding='10sp'
android:background="#color/yellow">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editText_email"
android:hint="Enter player email address" />
<ImageView
android:layout_width="50dp"
android:layout_height="48dp"
android:id="#+id/imgView_add"
android:src="#drawable/btn_create" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView_players"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/btn_confirm"
android:layout_gravity="center_horizontal"
android:src="#drawable/btn_confirm"
android:layout_weight="1" />
</LinearLayout>
Iv tried dialog builder and regular dialog libs but hit small issues in both.
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//remove the dialog title
dialog.setContentView(R.layout.dialog_inviteusers);
dialog.setTitle("Sign In");
//Hook up the dialog controls
ImageView btn_add = (ImageView) dialog.findViewById(R.id.imgView_add);
ListView listView_emails = (ListView) dialog.findViewById(R.id.listView_players);
ArrayList<String> list = new ArrayList<String>();
Adapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_expandable_list_item_1, list);
listView_emails.setAdapter((android.widget.ListAdapter) adapter);
// if button is clicked, close the custom dialog
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
dialog.dismiss();
}
});
dialog.show();

How can I create my own theme for alertDialog?

I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color, and set the edge of the alertDialog to a round shape.
Is it possible to create our own theme for alert dialogs, using any style or creating a class which extends AlertDialog.Builder? I need a common theme or style for my all instances of alertDialog. I am using alertDialog in many places - one for singleChoice items, one with ArrayAdapter.
My alertDialog with array adapter:
String[] items = {"Edit profile","Change user","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
settingMenu.setAdapter(adapter, listener);
My alertDialog with single choice items:
alertDelete = new AlertDialog.Builder(getParent());
alertDelete.setTitle("Delete");
alertDelete.setCancelable(true);
CharSequence[] choice = {"this user","All user"};
alertDelete.setSingleChoiceItems(choice, 0,
For my all alertDialog, I need a common theme, like:
please check this link. here am creating my own alertDialog class
and its a simple method if you reuse the alertDialog in many situation in your application
how Create setAdapter() for a AlertDialog
I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.
But am confusing on how we can replace the setAdapter() and setSingleChoice().
Here one example For Dialog:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Give a Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Put here any custom text!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
// if button is clickedthen dialog will closed
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Custom AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
builder = new AlertDialog.Builder(this);
builder.setView(urlayoutfile);
alertDialog = builder.create();
alertDialog.show();
To use a specific style, use
ctw = new ContextThemeWrapper(this, R.style.MyStyle);
new AlertDialog.Builder(ctw)
.setTitle(...)
You can use layout as Different themes, styles, Backgrounds
customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
style="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layoutsample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="#+id/dialogheaderimage1"
android:layout_width="50dp"
android:layout_height="48dp"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/dialogheadertext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialogcontentbg"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="#+id/dialogmessgetext1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="23dp"
android:text=""
android:gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_horizontal"
android:layout_marginTop="20dip"
android:orientation="horizontal">
<Button
android:id="#+id/dialogokbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#drawable/buttonanimation"
android:text="Retry"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/dialogcancelbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonanimation"
android:text="Report"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
customDialog.java
final Dialog favDialog = new Dialog(Myclass.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
favDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
favDialog.setContentView(R.layout.reloadurlpopuplayout);
favDialog.setCancelable(false);
ImageView dialogImage = (ImageView) favDialog
.findViewById(R.id.dialogheaderimage1);
dialogImage.setBackgroundResource(R.drawable.questionmark);
TextView dialogMesage = (TextView ) favDialog
.findViewById(R.id.dialogmessgetext1);
TextView dialogHeader = (TextView) favDialog
.findViewById(R.id.dialogheadertext1);
String descText = getString(R.string.RetryReportMessage);
dialogMesage.setBackgroundColor(0x00000000);
dialogMesage.setText(descText);
dialogHeader.setText(R.string.BrockenLinkHeader);
try {
favDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Custom Diaglog box android

i am trying to make a custom dialog box with 2 spinners and 2 buttons.
i am doing the following coding
this is xml for custom GUI inside dialog box
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SORT BY" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/prompt1"
android:entries="#array/ordersortby"
android:layout_gravity="center"/>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ORDER"
/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/address"
android:entries="#array/ordersortby1"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
in strings i am declaring following
<string name="prompt1">Order Number</string>
<string-array name="ordersortby">
<item>Order Number</item>
<item>Date Submitted</item>
<item>Date Entered</item>
</string-array>
<string-array name="ordersortby1">
<item>ASC</item>
<item>DESC</item>
</string-array>
and in activity i am doing following
final Dialog dialog = new Dialog(orders.this);
dialog.setContentView(R.layout.orderpicker);
dialog.setTitle("Sort By Dialog");
dialog.show();
when running this , i am getting this
spinner
my problem is why i am not getting any data inside these pickers. please help me.
Let use below code,
spinner_ordersortby = (Spinner)findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_language.setAdapter(adapter);
another thing is make sure your array should inside this directory
/res/values/arrays.xml
Use below code before dialog.show(); and define your string array into string.xml file, it will solve your problem.
Spinner mSpinner1 = (Spinner)dialog.findViewById(R.id.spinner_ordersortby);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.string.ordersortby, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner1.setAdapter(adapter);
in your code try something like this
LayoutInflater factory = LayoutInflater.from(this);
View myview = factory.inflate(R.layout.orderpicker, null);
Dialog dialog = new Dialog(orders.this);
dialog.setContentView(myview);
dialog.setTitle("Sort By Dialog");
dialog.show();
First get the view.
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.yourlayout, null);
Now use this view to declare your spinner.
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
This will solve your problem.

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?
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>
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.
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();
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:
You can use any layout for alert dialogs. If you want a listview I would do it like here

Categories

Resources