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); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Thank you very much! |
This comment has been minimized.
This comment has been minimized.
Yep so nice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Thank you: Very usefull work!