Question

I have a PreferenceScreen made with a custom ListPreference and a CheckBoxPreference. This is the XML called 'pref_screen_custom.xml':

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- My custom preference type.  This just replaces the actual widget
     portion of the preference, if the whole preference wanted to be
     replaced we would use the layout attribute instead of the widgetLayout
     attribute. -->
<com.pref_002.Pref002
        android:key="my_preference"
        android:title="Title: Pref002"
        android:summary="Summary: summary"
        android:defaultValue="-1"
        android:entries="@array/pref002_list_ent"
        android:entryValues="@array/pref002_list_vals"
        android:dialogTitle="title" />

<CheckBoxPreference
        android:key="advanced_checkbox_preference"
        android:title="Title: checkbox preference"
        android:summaryOn="On checkbox" 
        android:summaryOff="Off checkBox" />
</PreferenceScreen>

The XML that holds the array for the preferences is (array.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="pref002_list_ent">
        <item>T</item>
        <item>V</item>
        <item>S</item>
    </string-array>
    <string-array name="pref002_list_vals">
        <item>10</item>
        <item>8</item>
        <item>6</item>
    </string-array>

</resources>

The .java for the PreferenceScreen, called PrefCustom002Activity.java, is:

package com.pref_002;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefCustom002Activity extends PreferenceActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_screen_custom_001);
    }
}

And the class that extends ListPreference, called 'Prefs002.java', is:

package com.pref_002;

import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class Pref002 extends ListPreference
{
    // Constructor called by the inflater
    public Pref002(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    @Override
    protected View onCreateView(ViewGroup parent)
    {
        // New Layout that will contain the default loaded View from ListPreference
        // plus a new one (a Button)
        LinearLayout newLayoutParent = new LinearLayout(getContext());
        newLayoutParent.setOrientation(LinearLayout.HORIZONTAL);
        newLayoutParent.setWeightSum(10.0f);

        // get the View returned from ListPreference 
        View listPreferenceDefaultView = super.onCreateView(parent);

        // new layout values for this View
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params1.weight = 9.0f;

        listPreferenceDefaultView.setLayoutParams(params1);

        //--Button to be added to newLayoutParent --// 

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params2.gravity = Gravity.CENTER_VERTICAL;
        params2.weight = 1.0f;

        Button b = new Button(getContext());
        b.setText("A button");
        b.setLayoutParams(params2);


        // add the two views
        newLayoutParent.addView(listPreferenceDefaultView);
        newLayoutParent.addView(b);
        newLayoutParent.setId(android.R.id.widget_frame);

        return newLayoutParent;
    }
}

So, as you can see in 'Prefs002.java', I override onCreateView to make a new Layout for the ListPreference made of a LinearLayout that contains the View that the ListPreference makes itself plus a new Button, that is placed at the right of the View generated by the ListPreference. Graphically this works, BUT if I click on this new View, the dialog of the Preference is not shown (also when you click it, the row doesn't change to the yellow color, but I can fix that by adding a TouchListener). Also, according to the documentation, if I override OnCreateView I should set 'widget_frame' to the new View and I did it in the line newLayoutParent.setId(android.R.id.widget_frame); but that doesn't make the Dialog to appear when I click this View.

So, How can I call the Dialog of this custom ListPrerence?

Thanks in advance.

Was it helpful?

Solution

You can trigger the dialog by calling showDialog() (DialogPreference.showDialog). When you call super.onCreateView, the view returned will already have one with ID widget_frame.

I would suggest something like this:

View v = super.onCreateView(parent);
View widgetFrame = v.findViewById(android.R.id.widget_frame);
// add/remove stuff into widgetFrame here
return v;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top