Reputation: 193
I'm trying to follow the Android API guide on Settings, more specifically I'm trying to create this number picker dialog by following this part of the text. But I can't get it to work. When I press the preference that's supposed to launch the custom dialog, nothing happens. I assume I have to call something somewhere, but I can't figure out how.
Here's my MainActivity.java:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings_fragment:
// Display the fragment as the main content
Intent i = new Intent();
i.setClass(this, SettingsActivity.class);
startActivityForResult(i, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
And it launches SettingsActivity.java nicely. SettingsActivity.java:
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
}
}
Goes to SettingsFragment.java:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
Which launches preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="pref_key_category_settings"
android:title="@string/pref_category1_title" >
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_checkbox"
android:summary="@string/pref_checkbox_summ"
android:title="@string/pref_sync" />
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_checkbox_choose_int"
android:summary="@string/pref_checkbox_choose_string_summ"
android:title="@string/pref_choose_string" />
<Preference
android:dependency="pref_checkbox_choose_int"
android:key="pref_key_num_scroller"
android:summary="@string/pref_num_scroller_summ"
android:title="@string/pref_num_scroller"/>
</PreferenceCategory>
</PreferenceScreen>
As I said: nothing happens when I click pref_key_num_scroller.
I have no idea on how to call NumberPickerPreference.java:
public class NumberPickerPreference extends DialogPreference {
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.numberpicker_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
}
Upvotes: 1
Views: 1793
Reputation: 2573
Change Preference to NumberPickerPreference in your XML, using its fully qualified name:
<com.something.settings.NumberPickerPreference
android:dependency="pref_checkbox_choose_int"
android:key="pref_key_num_scroller"
android:summary="@string/pref_num_scroller_summ"
android:title="@string/pref_num_scroller"/>
Upvotes: 2