Skip to content Skip to sidebar Skip to footer

How To Change The TimePicker Font Color

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,M

Solution 1:

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


Post a Comment for "How To Change The TimePicker Font Color"