NumberPicker doesn't work inside DialogFragment

I am learning android programming basics and have some problems with NumberPicker.

I'm close to what I want, a dialog opens and the number and minute select buttons appear, but they don't have numbers above and below to scroll. I also cannot edit the numbers using the keyboard that appears. I tried to add an onValueChange listener, but it didn't fire any events when I used the keyboard.

Anyone have any advice on how to fix this? I noticed that there are many things I am trying to do with widgets in certain actions or fragments that are not supported in this particular class.

Below are snippets of relevant code and images:

I have a ClockActivity class with two buttons:

...
public class ClockActivity extends ActionBarActivity {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clock);

    // Define gui objects
    ...
    mWorkForTime           = (Button)findViewById(R.id.work_for_time);
    mBreakForTime          = (Button)findViewById(R.id.break_for_time);

    // Add listeners for the work for/break for buttons
    mWorkForTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Bring up the picker fragment
            DialogFragment newFragment = TimePickerFragment.newInstance(R.string.work_title);
            newFragment.show(getSupportFragmentManager(), "WorkTimePicker");
        }
    });
    mBreakForTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Bring up the picker fragment
            DialogFragment newFragment = TimePickerFragment.newInstance(R.string.break_title);
            newFragment.show(getSupportFragmentManager(), "BreakTimePicker");
        }
    });
    ...

      

The TimePickerFragment class extends the Dialog:

public class TimePickerFragment extends DialogFragment {
    // Setup the time pickers
    private NumberPicker mHourPicker;
    private NumberPicker mMinutePicker;

    public static TimePickerFragment newInstance(int title) {
        TimePickerFragment fragment = new TimePickerFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);

        int title = getArguments().getInt("title");

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = (View)inflater.inflate(R.layout.activity_time_picker, null);

        // Initialize the pickers
        mHourPicker = (NumberPicker)view.findViewById(R.id.hours_picker);
        mMinutePicker = (NumberPicker)view.findViewById(R.id.minutes_picker);
        mHourPicker.setMinValue(0);
        mMinutePicker.setMinValue(0);
        mHourPicker.setMaxValue(24);
        mMinutePicker.setMaxValue(59);


        Dialog alertDialog = new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setView(inflater.inflate(R.layout.activity_time_picker, null))
                .setNegativeButton(R.string.negate,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ((ClockActivity) getActivity()).doNegativeTimeDialogClick();
                            }
                        })
                .setPositiveButton(R.string.confirm,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ((ClockActivity) getActivity()).doPositiveTimeDialogClick();
                            }
                        })
                .create();
        return alertDialog;
    }
}

      

Below is the XML to represent the DialogFragment found in

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/hours"
                android:textSize="24sp"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/minutes"
                android:textSize="24sp"
                android:textStyle="bold"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <NumberPicker
                android:id="@+id/hours_picker"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>

            <NumberPicker
                android:id="@+id/minutes_picker"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>

        </LinearLayout>
    </LinearLayout>

      

Here is a picture of the current operation:

http://i.imgur.com/4UiSLcn.png

This is what I want to have:

http://www.i-programmer.info/images/stories/Core/Android/Pickers/ten.gif

This is the output I see in the console when I try to edit the NumberPicker using the soft keyboard:

01-02 16:10:46.559  18438-18438/com.example.android.worktimer W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection
01-02 16:10:46.569  18438-18438/com.example.android.worktimer W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection

      

+3


source to share


1 answer


You must provide the collector with an array of what you are trying to select.

    String[] myStringArray = new String[]{"a","b","c"};
    mMinutePicker.setMinValue(0);
    mMinutePicker.setMaxValue(myStringArray.length - 1);
    mMinutePicker.setDisplayedValues(myStringArray);

      

And change

    Dialog alertDialog = new AlertDialog.Builder(getActivity()) .setTitle(title) .setView(inflater.inflate(R.layout.activity_time_picker, null)) 

      



to

    Dialog alertDialog = new AlertDialog.Builder(getActivity()).setTitle(title).setView(view)

      

You're fanning the show again.

+2


source







All Articles