Example usage for android.widget NumberPicker setDescendantFocusability

List of usage examples for android.widget NumberPicker setDescendantFocusability

Introduction

In this page you can find the example usage for android.widget NumberPicker setDescendantFocusability.

Prototype

public void setDescendantFocusability(int focusability) 

Source Link

Document

Set the descendant focusability of this view group.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Object showNativePicker(final int type, final Component source, final Object currentValue,
        final Object data) {
    if (getActivity() == null) {
        return null;
    }/*from w w  w .  j a v  a2  s. c o  m*/
    final boolean[] canceled = new boolean[1];
    final boolean[] dismissed = new boolean[1];

    if (editInProgress()) {
        stopEditing(true);
    }
    if (type == Display.PICKER_TYPE_TIME) {

        class TimePick
                implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnCancelListener, Runnable {
            int result = ((Integer) currentValue).intValue();

            public void onTimeSet(TimePicker tp, int hour, int minute) {
                result = hour * 60 + minute;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            @Override
            public void onCancel(DialogInterface di) {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final TimePick pickInstance = new TimePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                int hour = ((Integer) currentValue).intValue() / 60;
                int minute = ((Integer) currentValue).intValue() % 60;
                TimePickerDialog tp = new TimePickerDialog(getActivity(), pickInstance, hour, minute, true) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                //DateFormat.is24HourFormat(activity));
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        return new Integer(pickInstance.result);
    }
    if (type == Display.PICKER_TYPE_DATE) {
        final java.util.Calendar cl = java.util.Calendar.getInstance();
        if (currentValue != null) {
            cl.setTime((Date) currentValue);
        }
        class DatePick
                implements DatePickerDialog.OnDateSetListener, DatePickerDialog.OnCancelListener, Runnable {
            Date result = (Date) currentValue;

            public void onDateSet(DatePicker dp, int year, int month, int day) {
                java.util.Calendar c = java.util.Calendar.getInstance();
                c.set(java.util.Calendar.YEAR, year);
                c.set(java.util.Calendar.MONTH, month);
                c.set(java.util.Calendar.DAY_OF_MONTH, day);
                result = c.getTime();
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void onCancel(DialogInterface di) {
                result = null;
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }
        }
        final DatePick pickInstance = new DatePick();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                DatePickerDialog tp = new DatePickerDialog(getActivity(), pickInstance,
                        cl.get(java.util.Calendar.YEAR), cl.get(java.util.Calendar.MONTH),
                        cl.get(java.util.Calendar.DAY_OF_MONTH)) {

                    @Override
                    public void cancel() {
                        super.cancel();
                        dismissed[0] = true;
                        canceled[0] = true;
                    }

                    @Override
                    public void dismiss() {
                        super.dismiss();
                        dismissed[0] = true;
                    }

                };
                tp.setOnCancelListener(pickInstance);
                tp.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        return pickInstance.result;
    }
    if (type == Display.PICKER_TYPE_STRINGS) {
        final String[] values = (String[]) data;
        class StringPick implements Runnable, NumberPicker.OnValueChangeListener {
            int result = -1;

            StringPick() {
            }

            public void run() {
                while (!dismissed[0]) {
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException er) {
                        }
                    }
                }
            }

            public void cancel() {
                dismissed[0] = true;
                canceled[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            public void ok() {
                canceled[0] = false;
                dismissed[0] = true;
                synchronized (this) {
                    notify();
                }
            }

            @Override
            public void onValueChange(NumberPicker np, int oldVal, int newVal) {
                result = newVal;
            }
        }

        final StringPick pickInstance = new StringPick();
        for (int iter = 0; iter < values.length; iter++) {
            if (values[iter].equals(currentValue)) {
                pickInstance.result = iter;
                break;
            }
        }
        if (pickInstance.result == -1 && values.length > 0) {
            // The picker will default to showing the first element anyways
            // If we don't set the result to 0, then the user has to first
            // scroll to a different number, then back to the first option
            // to pick the first option.
            pickInstance.result = 0;
        }

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                NumberPicker picker = new NumberPicker(getActivity());
                if (source.getClientProperty("showKeyboard") == null) {
                    picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
                }
                picker.setMinValue(0);
                picker.setMaxValue(values.length - 1);
                picker.setDisplayedValues(values);
                picker.setOnValueChangedListener(pickInstance);
                if (pickInstance.result > -1) {
                    picker.setValue(pickInstance.result);
                }
                RelativeLayout linearLayout = new RelativeLayout(getActivity());
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
                RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

                linearLayout.setLayoutParams(params);
                linearLayout.addView(picker, numPicerParams);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
                alertDialogBuilder.setView(linearLayout);
                alertDialogBuilder.setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                pickInstance.ok();
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                pickInstance.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });
        Display.getInstance().invokeAndBlock(pickInstance);
        if (canceled[0]) {
            return null;
        }
        if (pickInstance.result < 0) {
            return null;
        }
        return values[pickInstance.result];
    }
    return null;
}