Omar
Omar

Reputation: 8145

Customize ListView in AlertDialog

Hey, I have a ListView that I want to be displayed into an AlertDialog, I have 2 problems:
1. The text is displayed in the left side, I want it to be in the center
2. To choose an item, you have to select exactly the text of the row.. How to make it possible to select the item even if selecting outside the text in the same row?
You can see in the picture that the orange highlight is on the text itself only rather than the whole row..:
enter image description here

Code:

LinearLayout layout = new LinearLayout(mContext);

            ListView lv = new ListView(mContext);

            lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, BooksOptions));

            Builder alert;
            alert = new AlertDialog.Builder(this);              

            layout.addView(lv);
            alert.setView(layout);

Upvotes: 2

Views: 2322

Answers (1)

Brian Dupuis
Brian Dupuis

Reputation: 8176

You're utilizing the standard Android list item layout, android.R.layout.simple_list_item_1. Instead, create your own item layout based on that layout and set the properties appropriately.

<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:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center" android:paddingLeft="6dip" android:minHeight="?android:attr/listPreferredItemHeight" />

Note that you're also not setting layout properties on the ListView itself. Ensure that you specify it to have it match_parent for width as well in your layout.

Upvotes: 2

Related Questions