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 .
Problems and disadvantage to use android native
date and time picker
.pre lollipop
andlollipop
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.
BaseProject.java
Add this dependency
You can change font colors, background colors, date time text colors as explained in link