I am building an AlertDialog to show a menu where the user can select the country where his mobile phone number belongs.
An example of the list is somehting like this:
-----------------------------
| PHONE PREFIX |
| |
| Spain - 0034 |
| United Kingdom - 0044 |
| France - 0033 |
|---------------------------|
| CANCEL ACCEPT |
|----------------------------
and so on...
I have created a list_view item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_listview_pais"
android:textSize="20sp"
android:text="United Kingdom"
android:layout_alignParentLeft="true"
android:textColor="@color/cardview_dark_background"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_listview_guion"
android:textSize="20sp"
android:text="-"
android:layout_centerHorizontal="true"
android:textColor="@color/cardview_dark_background"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_listview_prefijo"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:textColor="@color/cardview_dark_background"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_alignBaseline="@+id/item_listview_pais"
android:text="0034"
/>
</RelativeLayout>
</RelativeLayout>
Also the layout I want for the AlertDialog which is very simple:
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/input_turno_dialog_listview_prefijos">
</ListView>
</ScrollView>
</LinearLayout>
At some point on my activity, I press a button and inside the actionbutton event I call a PrefijosDS which give me an ArrayList to populate the adapter. Prefijo class is like this:
private String Country;
private String Prefijo;
so a Prefijo object can be something like this:
Prefijo prefijo = new Prefijo("Spain","0034");
With my PrefijoDS I get an Array so I get the data I need. Then, I call the AlertDialog like this:
AlertDialog alertDialog = builder.create();
ListView lv = (ListView) convertView.findViewById(R.id.input_turno_dialog_listview_prefijos);
PrefijoDS prefijoDS = new PrefijoDS(getApplicationContext());
ListaPrefijosAdapter prefijosAdapter = new ListaPrefijosAdapter(getApplicationContext());
prefijosAdapter.set((ArrayList<Prefijo>) prefijoDS.obtenerPrefijos());
lv.setAdapter(prefijosAdapter);
alertDialog.show();
It works, I get the AlertDialog shown but the height of the ListView is very small, almost the height of a single row item. What I want is resize height a bit so it can show 3 or 4 items on the list and then the rest can be scrolled. I tried different ways to make a highest AlertDialog but every time I get the same size. The only way I get an AlertDialog bigger is when I don't use the ScrollView. However, this turns into a very big AlertDialog which I don't want.
I've tried to do this:
alertDialog.getWindow().setLayout(200,300);
but then result is a resized AlertDialog but the ListView remains the same. How can I do it?
Thanks a lot!
Change Alert Dialog Size
AlertDialog.Builder adb = new AlertDialog.Builder(this);
Dialog d = adb.setView(new View(this)).create();
// (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
And one more thing change list view layout_height and layout_width to match_parent
You can use weight
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/input_turno_dialog_listview_prefijos">
</ListView>
</LinearLayout>
<Button
android:id="@+id/btn_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAVE" />
</LinearLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.