-1

Working in an android app,Here I have a TimePicker with spinner mode and my layout background color is black.So I want to change the color of the TimePicker's font to white [Hour,Minute,AM/PM].

I have referred and tried so many solutions for this from SO ,But nothing is solved my problem.

Finally I found a source Change the text color of NumberPicker

I have implemented as follows

import java.lang.reflect.Field;

public class CheckOut extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_alarm);

 mTimePicker = (TimePicker) findViewById(R.id.mTimePicker);
int mycolor = getResources().getColor(R.color.tab_background);
 setTimePickerColor(mTimePicker,mycolor);

}

 public static boolean setTimePickerColor(TimePicker timePicker, int color)
    {
        final int count = timePicker.getChildCount();
        for(int i = 0; i < count; i++){
            View child = timePicker.getChildAt(i);
            if(child instanceof EditText){
                try{
                    Field selectorWheelPaintField = timePicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint)selectorWheelPaintField.get(timePicker)).setColor(color);
                    ((EditText)child).setTextColor(color);
                    timePicker.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;
    }
}

I don't know where I did the mistake ,Because it is not make any effect .

Nikson
  • 820
  • 2
  • 16
  • 43

1 Answers1

0

Problems and disadvantage to use android native date and time picker.

  1. Customisation is hard enough that one will never want to spend hours on date picker.
  2. Date and Timer picker UI varies in pre lollipop and lollipop device. (I mention this)

Solution

I always use MaterialDateTimePicker library to get rid of this problem. This library is highly customisable, well maintained repo.

I am sharing you my base activity class code, that you can add in your base activity class or make a new one.

You will extend your activity class by this base class. and just call simple method that i wrote.

openTimeDialog(Calendar.getInstance(), new OnTimeSelected() {
            @Override
            public void onTimeSet(Calendar timeCalendar) {

            }
        });

BaseProject.java

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.Calendar;

/**
 * Created by KHEMRAJ on 8/1/2017.
 */

public class BaseProject extends Activity {

    public interface OnDateSelected {
        void onDateSet(Calendar dateCalendar);
    }

    public interface OnTimeSelected {
        void onTimeSet(Calendar timeCalendar);
    }

    public void openDateDialog(OnDateSelected onDateSelected) {
        openDateDialog(Calendar.getInstance(), null, null, onDateSelected);
    }

    public void openDateDialog(@NonNull Calendar preSelectedCalendar, final OnDateSelected onDateSelected) {
        openDateDialog(preSelectedCalendar, null, null, onDateSelected);
    }

    public void openDateDialog(@NonNull Calendar preSelectedCalendar, @Nullable Calendar minDateCalander, final OnDateSelected onDateSelected) {
        openDateDialog(preSelectedCalendar, minDateCalander, null, onDateSelected);
    }

    public void openDateDialog(@NonNull Calendar preSelectedCalendar, @Nullable Calendar minDateCalander, @Nullable Calendar maxDateCalander, final OnDateSelected onDateSelected) {
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, monthOfYear);
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        if (onDateSelected != null) onDateSelected.onDateSet(calendar);
                    }
                },
                preSelectedCalendar.get(Calendar.YEAR),
                preSelectedCalendar.get(Calendar.MONTH),
                preSelectedCalendar.get(Calendar.DAY_OF_MONTH)
        );
        if (minDateCalander != null)
            dpd.setMinDate(minDateCalander);
        if (maxDateCalander != null)
            dpd.setMaxDate(maxDateCalander);
        dpd.show(getFragmentManager(), "Datepickerdialog");
    }

    public void openTimeDialog(@NonNull Calendar preSelectedCalendar, final OnTimeSelected onTimeSelected) {
        TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calendar.set(Calendar.MINUTE, minute);
                        calendar.set(Calendar.SECOND, second);
                        if (onTimeSelected != null) onTimeSelected.onTimeSet(calendar);
                    }
                },
                preSelectedCalendar.get(Calendar.HOUR_OF_DAY),
                preSelectedCalendar.get(Calendar.MINUTE),
                false);
        timePickerDialog.show(getFragmentManager(), "TimePickerDialog");
    }

}

Add this dependency

compile 'com.wdullaer:materialdatetimepicker:3.6.0'

You can change font colors, background colors, date time text colors as explained in link

Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182