I need to create a custom ListPreference dialog to add a title text (TextView) above the list (ListView). I created a class MyListPreference that extends ListPreference and overrides onCreateDialogView ():
@Override protected View onCreateDialogView() { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = (View) inflater.inflate(R.layout.dialog_preference_list, null); return v; }
My xml layout dialog_preference_list.xml contains:
<?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbarAlwaysDrawVerticalTrack="true" /> </LinearLayout>
Problem: TextView appears below ListView, not above. I need the TextView to be taller. I tried both with LinearLayout and RelativeLayout (using the attributes "below" or "above") without success: I cannot find a way to place the TextView over the ListView ... The layout is pretty simple and I can not see why the list remains above ... Also draw attention that the problem occurs both on a real device (Nexus 4, Android 4.2.2), and on the emulator. However, when viewing the layout presented in the Eclipse graphic layout, the layout is correct! See both attached images.
Any idea on how to solve this problem?
Layout displayed on the device (incorrect):

Layout made on Eclipse (correct):

Change using solution 10.07.2013
As suggested by the accepted answer, the problem is due to using builder.setSingleChoiceItems () in ListPreference onPrepareDialogBuilder (). I fixed this by expanding ListPreference and overriding onCreateDialogView () to build a dialog without a builder so that I can create a custom view that displays the title text above the list items.
GPListPreference.java:
public class GPListPreference extends ListPreference { ... @Override protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { builder.setNegativeButton(null, null); builder.setPositiveButton(null, null); } private int getValueIndex() { return findIndexOfValue(getValue()); } @Override protected View onCreateDialogView() { LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); ListView lv = (ListView) inflater.inflate(R.layout.dialog_preference_list, null); TextView header = (TextView) inflater.inflate(R.layout.dialog_preference_list_header, null); header.setText(getDialogMessage());
dialog_preference_list.xml:
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbarAlwaysDrawVerticalTrack="true" />
dialog_preference_list_singlechoice.xml
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView 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:checkMark="?android:attr/listChoiceIndicatorSingle" android:ellipsize="marquee" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:paddingBottom="2dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="2dip" android:textAppearance="?android:attr/textAppearanceMedium" />
dialog_preference_list_header.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:textAppearance="?android:attr/textAppearanceSmall"> </TextView>