Created
May 6, 2011 22:06
-
-
Save thom-nic/959884 to your computer and use it in GitHub Desktop.
NumberPicker Preference Dialog for Android!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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.
Thank you very much!
Yep so nice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you: Very usefull work!