I have seen many customized solutions and answers to this question. I need something very simple, I have a preference activity and all I need is that one of the options will open dialog with a number picker and save the results. Can you please guide me step by step with how to accomplish this?
public class SettingsActivity extends PreferenceActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
//requestWindowFeature(Window.FEATURE_NO_TITLE);
}
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
XML:
<SwitchPreference
android:key="cross"
android:summaryOff="Cross is invisible"
android:summaryOn="Cross is visible"
android:switchTextOff="OFF"
android:switchTextOn="ON"
android:title="Cross"
android:defaultValue="true"/>
<SwitchPreference
android:key="autoP"
android:summaryOff="App will go to sleep"
android:summaryOn="App will not go to sleep"
android:switchTextOff="OFF"
android:switchTextOn="ON"
android:title="Always On"
android:defaultValue="true"/>
<SwitchPreference
android:key="tempD"
android:summaryOff="Temprature not displayed"
android:summaryOn="Temprature displayed"
android:switchTextOff="OFF"
android:switchTextOn="ON"
android:title="Tempature Display"
android:defaultValue="true"/>
<ListPreference
android:entries="@array/units"
android:entryValues="@array/lunits"
android:key="listUnits"
android:summary="Units schosssing"
android:title="Units" android:defaultValue="C"/>
<!--Need to add button to open dialog-->
</PreferenceScreen>
Number Picker XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/numberPicker1"
android:layout_marginLeft="20dp"
android:layout_marginTop="98dp"
android:layout_toRightOf="@+id/numberPicker1"
android:text="Cancel" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="@+id/numberPicker1"
android:text="Set" />
</RelativeLayout>
Subclass
DialogPreference
to build your ownNumberPickerPreference
.I have implemented one below for you. It works perfectly fine, but is not feature complete. For example the minimum and maximum values are hard-coded constants. These should really be attributes on the preference xml declaration. To get that to work you would need to add an attrs.xml file specifying your custom attributes.
For the full implementation of the NumberPicker preference widget that supports custom xml attributes in a library project and a demo app showing how to use it, see GitHub: https://github.com/Alobar/AndroidPreferenceTest
You would use the widget as any other preference widget, except you have to fully qualify the name:
preferences.xml
NumberPickerPreference.java
Implementing
DialogPreference
is a solution: