Custom Diaglog box android - 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.

Related

Android spinner don't show selected text while list appears in drop down

i am newbie in android and i am trying to use two spinner views in an activity. entries are showing in drop down of spinner but when i select an entry, it doesn't appear in spinner control. I have searched all over but it didn't work for me. following is all i am doing/trying.
My activity xml is as
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_seventy_thirty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.elecsysindia.montestapp.SeventyThirtyActivity">
<TextView
android:text="Division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDivison"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerDivision"
android:layout_marginLeft="32dp"
android:layout_alignTop="#+id/txtDivison"
android:layout_toRightOf="#+id/txtDivison"
android:layout_toEndOf="#+id/txtDivison" />
<TextView
android:text="Station"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtDivison"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:id="#+id/txtStationCode"
android:textSize="20sp"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerStation"
android:layout_below="#+id/spinnerDivision"
android:layout_alignLeft="#+id/spinnerDivision"
android:layout_alignStart="#+id/spinnerDivision"
android:layout_marginTop="16dp"
android:layout_marginStart="48dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="48dp" />
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSubmit"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_alignLeft="#+id/spinnerStation"
android:layout_below="#+id/spinnerStation"
/>
</RelativeLayout>
and in class:
private static List<String> listDivisions = new ArrayList<String>();
private List<String> listStation = new ArrayList<>();
Spinner spinnerDivision ;
Spinner spinnerStation;
Button btnSubmit;
ArrayAdapter<String> adapterDivision;
ArrayAdapter<String> adapterStation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seventy_thirty);
spinnerDivision = (Spinner) findViewById(R.id.spinnerDivision);
spinnerStation = (Spinner) findViewById(R.id.spinnerStation);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
// adapterDivision = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listDivisions);
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
adapterDivision.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDivision.setAdapter(adapterDivision);
adapterDivision.notifyDataSetChanged();
adapterStation = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,listStation);
adapterStation.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStation.setAdapter(adapterStation);
adapterStation.notifyDataSetChanged();
And this is how lists are updated:
for (int i = 0; i < subs.length(); i++){
JSONObject subsDetail = subs.getJSONObject(i);
ArrayList<String> stnList = new ArrayList<String>();
if(subsDetail != null){
stnList.clear();
String DivCode = subsDetail.getString("divCode");
JSONArray st = subsDetail.getJSONArray("stncode");
String city="";
for (int j=0 ; j<st.length(); j++){
stnList.add(st.getString(j));
city = city.concat(st.getString(j)+"-");
}
mapSubscriptions.put(DivCode,stnList);
listDivisions.add(DivCode);
listStation.addAll(stnList);
I am getting data from simple php script and this data is getting inserted to lists.
Also after searching related issue in Sof, i have tried to use a separate layout for spinner as used in
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
but still of no use.
xml file for layout_spinner_item is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinnerItem"
android:textSize="20sp"
android:gravity="left"
android:textColor="#aaddaa"
android:padding="5dip"
/>
I even tried to make text color change in theme itself by setting property android:textColor. but again no success.
<item name="android:textColor">#FF00FF</item>
can you please help me why text is not appearing in spinner control.
After a lot of struggle, text in one spinner is appearing but that appears only if i restart activity directly , i mean without navigating to activity from main activity.
second spinner (spinnerStation) dont show the text at all. even same source code for both spinners. please see screen shots.
text appear in spinnerDivision
Entries of spinnerStation which dont show text
You have to programmatically select the position.
Like this
spinnerObject.setSelection(INDEX);
And, you should change this
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),R.layout.layout_spinner_item,listDivisions);
to this,
adapterDivision = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,listDivisions);
The problem is, your adapter is not getting TextView from custom layout file

Change color textview in adapter spinner android

reports = (Spinner)findViewById(R.id.spinner_report);
reportType = getIntent().getStringExtra("report");
private void setTypeReport(){
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.type_report, R.layout.item_spinner_p);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reports.setAdapter(adapter);
int pos= adapter.getPosition(reportType);
type_report= getResources().getStringArray(R.array.type_report);
String valueAtIndex = type_report[pos];
for(int i = pos; i > 0; i--){
type_report[i] = type_report[i-1];
}
type_report[0] = valueAtIndex;
//now set this array to second Spinner
ArrayAdapter spinnerBArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
type_report);
reports.setAdapter(spinnerBArrayAdapter);
newreport = reports.getSelectedItem().toString();
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#eee"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_report"
android:background="#drawable/round_box"
android:visibility="gone"
android:layout_marginBottom="5dp">
</Spinner>
</RelattiveLayout>
item_spinner_p.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
string.xml
<string-array name="type_report">
<item>Male</item>
<item>Female</item>
</string-array>
Default color is black in item_spinner_p.xml ... either male or female , it will show black color.. Problem is , I want to do when reportType = Male (from previous activity), it will change color to Red and if reportType = Female (from previous activity) , it change color to Blue.
I think to use way set color in string.xml .. set color Red for Male .. but i dont know correct way to set it ..
You have to create custom spinner adapter. Look at this page for how to do that: http://mrbool.com/how-to-customize-spinner-in-android/28286
But if you want simple solution you can create two separate layouts for male and females like this :
For males: item_males
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/red"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
And for females: item_females
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/blue"
android:textSize="17sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"/>
With text colors set to red and blue for males and females respectively. Then apply a check while setting the adapter like this :
ArrayAdapter<CharSequence> adapter;
if(reportType.equals("Males")){
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_males);}
else{
adapter = ArrayAdapter.createFromResource(this,R.array.type_report, R.layout.item_females);}

Spinners in AlertDialog

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);

Two custom spinner layouts

I am trying to use two spinners with a custom dropdown yet, only the bottom one is showing with the custom layout when pulled down. I have been trying on my own to figure out why but I cannot.
public class setup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setuplayout);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.numberPlayers, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner02);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.gameDifficulty, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
}
}
This is the xml code for spinner_item mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#F9B12F"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:padding="5dip"
android:popupBackground="#000000"
android:background="#000000"
/>
This is the setuplayout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:id="#+id/mainSetupImage"
android:src="#drawable/setup"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<ImageView
android:id="#+id/players"
android:clickable="true"
android:src="#drawable/players"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true">
</ImageView>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner01"
android:textColor="#F9B12F"
android:layout_toRightOf="#+id/players"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:background="#drawable/spinner"/>
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:drawSelectorOnTop="true"
android:id="#+id/Spinner02"
android:textColor="#F9B12F"
android:layout_below="#+id/Spinner01"
android:layout_toRightOf="#+id/difficulty"
android:layout_marginTop="25dp"
android:background="#drawable/spinner"/>
<ImageView
android:id="#+id/difficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/Spinner02"
android:clickable="true"
android:src="#drawable/difficulty" />
</RelativeLayout>
in 2nd spinner u used adapter rather than adapter2. so use adapter2 and run your code hope it will run. :)
I figured it out with your help. I had used the code "adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)" which was overriding my custom layout. Thanks for highlighting where the two codes were different. That was a huge help. I deleted the quoted lines and it now works.
I'd like to share a little code that will make all people's code much cleaner when making ArrayAdapters.
Just define a static method in a class of your preference (even MainActivity...) to give you the Array adapter without dirting so much your code:
public static ArrayAdapter<CharSequence>
getArrayAdapter( Context c, int arrayId, int idLayout1, int idLayout2){
ArrayAdapter<CharSequence> aa;
aa = ArrayAdapter.createFromResource( c, arrayId , idLayout1);
aa.setDropDownViewResource( idLayout2);
return aa;
}
Then in your code you just need to do it:
myspinner.addAdapter(
this, ThatClass.getArrayAdapter(
R.array.myid, R.layout.id1, R.layout.id2 );
myspinner.setOnItemSelectedListener(this);
/* Remember that the default Android Spinner layout can be get passing these specific layout ids:
android.R.layout.simple_spinner_item as layout1
android.R.layout.simple_spinner_dropdown_item as layout2
*/

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