Create NumberPicker dialog in preference
Asked Answered
T

4

8

I am trying to create a NumberPicker dialog in my preference screen. I have already made one following this:https://mcmap.net/q/259930/-timepicker-in-preferencescreen

However, for my second dialog, I only want one spinner, so I have adapted the code as follows:

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;

public class SnoozeTPP extends DialogPreference { 

    private int Minute = 0;
    private NumberPicker np= null;

    public static int getMinute(String time) {
        String[] pieces = time.split(":");

        return (Integer.parseInt(pieces[1]));
    }

    public SnoozeTPP(Context context, AttributeSet attrs) {
        super(context, attrs);

        setPositiveButtonText("Set"); 
        setNegativeButtonText("Cancel"); 
    }

    @Override
    protected View onCreateDialogView() {
        np = new NumberPicker(getContext());

        return (np);
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);

        np.setMaxValue(60);
        np.setValue(Minute);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {                                                             
        super.onDialogClosed(positiveResult);

        if (positiveResult) {

            Minute = np.getValue();

            String time = 0 + ":" + String.valueOf(Minute);

            if (callChangeListener(time)) {
                persistString(time);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return (a.getString(index));
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        String time = null;

        if (restoreValue) {
            if (defaultValue == null) {
                time = getPersistedString("08:00");
            } else {
                time = getPersistedString(defaultValue.toString());
            }
        } else {
            time = defaultValue.toString();
        }

        Minute = getMinute(time);
    }

}

There are no errors and the dialog pops up correctly, but the layout of it seems to be "messed up" :-). The blue line stretch across the whole dialog instead of just the width of the numbers. enter image description here

The question is - how to set the layout correctly? (I am sure there are lots of other mistakes as well!)

Thank you

Tarkington answered 7/7, 2013 at 18:35 Comment(2)
may help you gist.github.com/tomstrummer/959884Ladew
@RSenApps Thank you, I did try that but I couldn't get it to work :-(Tarkington
T
5

I solved this by using the CyanogenMod number picker. Java file:

https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/src/com/cyanogenmod/trebuchet/preference/NumberPickerPreference.java

XML file: https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/layout/number_picker_dialog.xml

Attributes: https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/values/attrs.xml#L158

Tarkington answered 22/2, 2014 at 9:31 Comment(2)
Styleable is here: github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/…Algy
This class appears to reference the com.android.internal package. Do I need to build an original-android.jar as described here to use it, or am I overly complicating this?Blackbeard
N
3

Here is an example of simple, but working NumberPickerPreference, saving integer value between 1 and 100:

app screenshot

NumberPickerPreference.java:

public class NumberPickerPreference extends DialogPreference {
    private NumberPicker mPicker;
    private Integer mNumber = 0;

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected View onCreateDialogView() {
        mPicker = new NumberPicker(getContext());
        mPicker.setMinValue(1);
        mPicker.setMaxValue(100);
        mPicker.setValue(mNumber);
        return mPicker;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // needed when user edits the text field and clicks OK
            mPicker.clearFocus();
            setValue(mPicker.getValue());
        }
    }   

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mNumber) {
            mNumber = value;
            notifyChanged();
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 0);
    }
}
Numbers answered 24/5, 2015 at 16:27 Comment(0)
S
1

This is more a workaround than a solution, but i hope it helps. Adding a dummy textView solved the problem. I got exactly the same problem.

My xml File:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textDummyEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textDummyEmpty" />

    <NumberPicker 
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" /> 

</LinearLayout>

and

android:text="@string/textDummyEmpty" 

is an empty String. Maybe its also enough to use just a view instead of a textView.

Shadwell answered 31/7, 2013 at 9:36 Comment(7)
Thanks for your answer. In the end I used a different method to create it - it was open source code. I am not able to try what you suggested at the moment - but I will try to do so soon :-)Tarkington
@RiThBo please share how you solved the problem so other users can do it as well.Von
@Richard I used the CyanogenMod one. Its on GitHub somewhere, I looked for the exact file but i can't find it any more. SorryTarkington
hey @RiThBo, how did you solve this issue of the blue lines stretching all the way across the dialog?Coarctate
@toobsco42 I haven't found the link to the GitHub file, but I posted an answer containing most of the code.Tarkington
@Richard See my anwer if you are still having trouble.Tarkington
@RiThBo, where do you set that xml file? in what class?Coarctate
A
1

return a LinearLayout in onCreateDialogView rather than NumberPicker as below:

@Override
protected View onCreateDialogView() {
    numberPicker = new NumberPicker(getContext());
    numberPicker.setMinValue(1);
    numberPicker.setMaxValue(12);
    numberPicker.setWrapSelectorWheel(false);
    numberPicker.setValue(lastValue);

    LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pickerParams.gravity = Gravity.CENTER;
    numberPicker.setLayoutParams(pickerParams);

    LinearLayout layout = new LinearLayout(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(params);

    layout.addView(numberPicker);
    return layout;

    //return numberPicker;
}
Aborigine answered 19/12, 2014 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.