Tuesday, 24 May 2016

Customize TimePicker to NumberPicker and set Color



@Overridepublic void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        Class<?> classForid = Class.forName("com.android.internal.R$id");

        Field field = classForid.getField("minute");

        NumberPicker mMinuteSpinner = (NumberPicker) time_picker                .findViewById(field.getInt(null));
        mMinuteSpinner.setMinValue(0);
        mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
        List<String> displayedValues = new ArrayList<String>();
        for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
            displayedValues.add(String.format("%02d", i));
        }

        mMinuteSpinner.setDisplayedValues(displayedValues
                .toArray(new String[0]));

        setNumberPickerTextColor(mMinuteSpinner, getResources().getColor(R.color.black));

        //for Hours....................
        displayedValues.clear();
        field = classForid.getField("hour");

        mMinuteSpinner = (NumberPicker) time_picker                .findViewById(field.getInt(null));
        mMinuteSpinner.setMinValue(0);
        mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
        displayedValues = new ArrayList<String>();
        for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
            displayedValues.add(String.format("%02d", i));
        }

        mMinuteSpinner.setDisplayedValues(displayedValues
                .toArray(new String[0]));
        setNumberPickerTextColor(mMinuteSpinner, getResources().getColor(R.color.black));

        // for Seconds.............

        displayedValues.clear();

        field = classForid.getField("amPm");
        mMinuteSpinner = (NumberPicker) time_picker                .findViewById(field.getInt(null));
        setNumberPickerTextColor(mMinuteSpinner, getResources().getColor(R.color.black));
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
    final int count = numberPicker.getChildCount();
    for (int i = 0; i < count; i++) {
        View child = numberPicker.getChildAt(i);
        if (child instanceof EditText) {
            try {
                Field selectorWheelPaintField = numberPicker.getClass()
                        .getDeclaredField("mSelectorWheelPaint");
                selectorWheelPaintField.setAccessible(true);
                ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                ((EditText) child).setTextColor(color);
                numberPicker.invalidate();
                return true;
            } catch (NoSuchFieldException e) {
                Log.w("setNumberPickerTextColor", e);
            } catch (IllegalAccessException e) {
                Log.w("setNumberPickerTextColor", e);
            } catch (IllegalArgumentException e) {
                Log.w("setNumberPickerTextColor", e);
            }
        }
    }
    return false;
}

No comments:

Post a Comment