Search code examples
javaandroidxmlandroid-preferences

preferences.xml and extending DialogPreference


I'm trying to extend the DialogPreference so I can have a NumberPicker for one of my preferences. I've been reading:

http://developer.android.com/guide/topics/ui/settings.html#Custom

But I'm hitting an 'Error inflating NumberPickerReference' message. I think I know why, but I'm not sure how to fix it.

My preferences.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference
        android:key="pref_alertsEnabled"
        android:title="@string/alert_enable"
        android:summary="@string/alert_enable_desc"
        android:defaultValue="true" />

    <NumberPickerPreference 
        android:key="perf_Timer" 
        android:title="@string/timer" 
        android:negativeButtonText="@android:string/cancel" 
        android:dialogTitle="@string/timer" 
        android:summary="@string/timer_desc" 
        android:dialogMessage="@string/timer_desc" 
        android:positiveButtonText="@android:string/ok"/>

</PreferenceScreen>

And in my projects src folder I have NumberPickerReference.java. I think the thing I'm missing is how to tell Android where to find NumberPickerReference class.

When preferences.xml is read in, I'm guessing it goes looking for the NumberPickerReference class, but doesn't find it.

Do I need to specify where to find the NumberPickerReference.class ? If yes, how do I do this ?

BTW, the app runs fine if I remove the <NumberPickerReferecnes> item from preferences.xml


Solution

  • Just add the path to that preference:

    com.your.package_name.and_path.NumberPickerPreference
    

    E.g.

    <com.example.myapp.preferences.NumberPickerPreference
        android:key="perf_Timer" 
        android:title="@string/timer" 
        android:negativeButtonText="@android:string/cancel" 
        android:dialogTitle="@string/timer" 
        android:summary="@string/timer_desc" 
        android:dialogMessage="@string/timer_desc" 
        android:positiveButtonText="@android:string/ok"/>