Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Created May 6, 2011 22:06
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save thom-nic/959884 to your computer and use it in GitHub Desktop.
Save thom-nic/959884 to your computer and use it in GitHub Desktop.
NumberPicker Preference Dialog for Android!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation='vertical'
android:layout_width='wrap_content'
android:layout_height='wrap_content'
android:padding='3dp'
android:paddingLeft='8dp'
android:paddingRight='8dp'>
<!-- This layout is used by setting the 'dialogLayout' attribute in DialogPreference -->
<!-- This is filled by the 'message' attribute on DialogPreference: -->
<TextView android:id='@android:id/message'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:textAppearance='?android:attr/textAppearanceMedium'
android:layout_marginBottom='10dp' />
<com.quietlycoding.android.picker.NumberPicker
android:id="@+id/pref_num_picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import com.quietlycoding.android.picker.NumberPicker;
public class NumberPickerPreference extends DialogPreference {
NumberPicker picker;
Integer initialValue;
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
this.picker = (NumberPicker)view.findViewById(R.id.pref_num_picker);
// TODO this should be an XML parameter:
picker.setRange(1, 7);
if ( this.initialValue != null ) picker.setCurrent(initialValue);
}
@Override
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
if ( which == DialogInterface.BUTTON_POSITIVE ) {
this.initialValue = picker.getCurrent();
persistInt( initialValue );
callChangeListener( initialValue );
}
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue,
Object defaultValue) {
int def = ( defaultValue instanceof Number ) ? (Integer)defaultValue
: ( defaultValue != null ) ? Integer.parseInt(defaultValue.toString()) : 1;
if ( restorePersistedValue ) {
this.initialValue = getPersistedInt(def);
}
else this.initialValue = (Integer)defaultValue;
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 1);
}
}
@felixdivo
Copy link

Thank you: Very usefull work!

@jackmott
Copy link

It seems that a preference with a default and minimum value of 1 causes a bug where the preference is not available upon initial creation of the preference activity.

@moodpo
Copy link

moodpo commented Aug 8, 2013

Thank you very much!

@sapher
Copy link

sapher commented Sep 28, 2016

Yep so nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment