Issues trying to create a custom dialog preference that gets a time period
When (in which method) should I access the number pickers to harvest the values okay'ed?
Your TimePeriodPreference
class need to implements the DialogInterface.OnClickListener
and Override
the onClick
method like this.
Override
public void onClick(DialogInterface dialog, int which){
if(which == DialogInterface.BUTTON_POSITIVE) {
// do your stuff to handle positive button
}else if(which == DialogInterface.BUTTON_NEGATIVE){
// do your stuff to handle negative button
}
}
And then you can access the number picker when the Ok
button (BUTTON_POSITIVE)
is clicked.
EDIT:
Why are the number pickers in my custom dialog preference missing the familiar +- controls?
You need to set a Max and Minimum value to the picker like this, to display the increment and decrement values.
NumberPicker np = (NumberPicker)findViewById(R.id.picker);
np.setMaxValue(100);
np.setMinValue(0);
You need to set the default values for the picker in onCreateDialogView
. Only here the dialog view will be created.
@Override
protected View onCreateDialogView() {
View dialogView = super.onCreateDialogView();
// set the default values to the view
NumberPicker hourPicker = (NumberPicker) dialogView.findViewById(R.id.hour_picker);
hourPicker.setMinValue(0);
hourPicker.setMaxValue(23);
return dialogView;
}
Note: getDialog() will be valid only when you show the dialog.
I need to have a custom dialog in Preferences
Implement a DialogPreference for your custom layout and add that preference to your preference screen
Here is a good explanation of how to do this:
Concise way of writing new DialogPreference classes?
Android Edit Text Preference with own layout with no newValue on onPreferenceChange
To answer my own question:
First of all, in PreferenceScreen, you need to state:
<full.path.to.your.OwnLayoutClass
android:name="whatevever"
android:dialogLayout="@layout/your_own_layout" />
your_own_layout can be anything you'd like, linearlayout with of buttons, editTexts, according to your wishes.
Essential is the class representing your own preference Dialog. Here is a simple example of how to do it:
public class YourOwnLayoutClass extends DialogPreference {
private LinearLayout mView;
public YourOwnLayoutClass(Context context, AttributeSet attrs) {
super(context, attrs);
setPersistent(false);
setDialogLayoutResource(R.layout.your_own_layout);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mView = (LinearLayout) view;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
// get value from editFields, do whatever you want here :)
// you can acces them through mView variable very easily
}
}
}
Important references:
I need to have a custom dialog in Preferences
Concise way of writing new DialogPreference classes?
Android: launch a custom Preference from a PreferenceActivity
Related Topics
The Type Java.Lang.Charsequence Cannot Be Resolved in Package Declaration
Vertically Centering Text in HTML Table Cell in Java Jlabel
Difference Between Getexternalfilesdir and Getexternalstoragedirectory()
Android Studio Error: "Manifest Merger Failed: Apps Targeting Android 12"
Getstring Outside of a Context or Activity
Error Inflating When Extending a Class
Exporting and Running Unity3D Project to Android Studio
Classnotfoundexception: Didn't Find Class "Com.Google.Android.Gms.Ads.Adview"
Convert Base64 String to Image in Java
How to Generate a Qr Code for an Android Application