8

I have implemented a custom Date Picker wherein I need to have custom colors for the number picker (Date picker uses Number picker inside) inside it as follows: Expected

With the help of this answer I could make it look like this: Actual Result

But as you see the actual date is getting shown in the yellow color instead of white. Moreover, I tried doing it at run time like this:

npMonth.setOnValueChangedListener(new OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                EditText et = ((EditText) npMonth.getChildAt(0));
                et.setTextColor(getResources().getColor(R.color.gold));
            }
        });

But still no luck.

How to change just the center text color to white and keep the top and bottom text yellow?

Community
  • 1
  • 1
Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • See http://stackoverflow.com/a/15032921/544198 – PearsonArtPhoto Nov 12 '14 at 12:56
  • Well that's rather discouraging @PearsonArtPhoto. All is done except fzr the central text color and I'll have to switch to some third party libs. I think I should spend some more time fiddling with it. – Rajkiran Nov 12 '14 at 13:28
  • @Rajkiran:I am also having the same issue. Can you please update the answer if you get the solution for above – Krishna Prasad Jul 21 '15 at 13:17
  • @Rajkiran : Did you find the solution for your question ?? – codeRider Sep 14 '15 at 14:54
  • @codeRider and Krishna no luck folks.. unfortunately I haven't find any solution. – Rajkiran Sep 14 '15 at 15:07
  • Did not try much after reading this http://stackoverflow.com/a/15032921/840669 – Rajkiran Sep 14 '15 at 15:13
  • @Rajkiran I'm interested too for a solution. I just finished wrapping up a custom number picker library (https://github.com/KasualBusiness/MaterialNumberPicker) that let you customize some of the most wanted private attributes, such as text color, but I can't color the center text only... Would be nice to come up with a solution :) – Stephen Vinouze Oct 03 '15 at 13:00
  • I also want the solution please help. In custom library no option for keyboard – Rajesh N Oct 12 '17 at 09:32

1 Answers1

0

The easiest way to do this

setNumberPickerTextColor(mNumberPicker);

public void setNumberPickerTextColor(NumberPicker numberPicker){
    int color=Color.parseColor("#333333");

    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();
            }
            catch(NoSuchFieldException e){
                Log.e("setNumberPickerColor1", ""+e);
            }
            catch(IllegalAccessException e){
                Log.e("setNumberPickerColor2", ""+e);
            }
            catch(IllegalArgumentException e){
                Log.e("setNumberPickerColor3", ""+e);
            }
        }
    }
}
Mahendra Gohil
  • 380
  • 1
  • 3
  • 21
  • From Review: Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 09 '18 at 06:56