Skip to content

Commit

Permalink
added droidkit namesapce for xml attributes, number picker preference…
Browse files Browse the repository at this point in the history
… accepts xml attrs for startRange and endRange, also can be set in Java.
  • Loading branch information
Mike Novak committed Dec 28, 2010
1 parent d3ef827 commit 6c56789
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 4 additions & 2 deletions demos/res/xml/num_pick_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* limitations under the License.
*/
-->
<PreferenceScreen xmlns:droidkit="http://schemas.android.com/apk/res/org.droidkit"
<PreferenceScreen xmlns:droidkit="http://schemas.android.com/apk/res/org.droidkit.demos"
xmlns:android="http://schemas.android.com/apk/res/android">

<org.droidkit.preference.NumberPickerPreference
android:key="demo.number.preference"
android:title="Demo Number Picker"
android:summary="Shows how to implement a num picker" />
android:summary="Implement a num picker pref"
droidkit:startRange="-50"
droidkit:endRange="50" />

</PreferenceScreen>
24 changes: 24 additions & 0 deletions res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Copyright (C) 2010 Mike Novak <michael.novakjr@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<resources>
<declare-styleable name="droidkit">
<attr name="startRange" format="integer" />
<attr name="endRange" format="integer" />
</declare-styleable>
</resources>
15 changes: 14 additions & 1 deletion src/org/droidkit/preference/NumberPickerPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;
Expand All @@ -26,11 +27,19 @@

public class NumberPickerPreference extends DialogPreference {
private NumberPicker mPicker;
private int mStartRange;
private int mEndRange;

public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.droidkit);
mStartRange = arr.getInteger(R.styleable.droidkit_startRange, 0);
mEndRange = arr.getInteger(R.styleable.droidkit_endRange, 200);

setDialogLayoutResource(R.layout.number_picker_pref);

arr.recycle();
}

public NumberPickerPreference(Context context, AttributeSet attrs) {
Expand All @@ -44,8 +53,8 @@ public NumberPickerPreference(Context context) {
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);

mPicker = (NumberPicker) view.findViewById(R.id.pref_num_picker);
mPicker.setRange(mStartRange, mEndRange);
mPicker.setCurrent(getValue());
}

Expand All @@ -59,6 +68,10 @@ public void onClick(DialogInterface dialog, int which) {
}
}

public void setRange(int start, int end) {
mPicker.setRange(start, end);
}

private void saveValue(int val) {
getEditor().putInt(getKey(), val).commit();
notifyChanged();
Expand Down

0 comments on commit 6c56789

Please sign in to comment.