9

Is it possible to change color of the selected item in numberpicker so each time a new center child TextView appear change its color to whatever I like I did not find any style or API expose about this ?

4 Answers4

3

enter image description hereI tried to follow many answers and all of them were setting the whole number's color.

I figured out how to change color only for selected number. set OnTouchListener in your NumberPicker.

numberPicker.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(final View v, MotionEvent event) {
                        currentTouchAction = event.getAction();
                        if (currentTouchAction == MotionEvent.ACTION_UP) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (currentTouchAction == MotionEvent.ACTION_UP) {
                                        setSelectedTextColor((NumberPicker)v, selectedColorRes);
                                    }
                                }
                            }, 300);
                        }
                        return false;
                    }
                });

and below code is for changing the selected number color. You might have to use 'performClick()' to dynamically apply the color.

And if you do that, the picker would immediately stop. So that's why I put postDelayed(300) above. User might drag number picker several times to select the profit number. If you put above postDelay() and wait for several milliseconds, it can be scrolled smoothly.

public void setSelectedTextColor(NumberPicker np, int colorRes) {
    final int count = np.getChildCount();
    for(int i = 0; i < count; i++){
        View child = np.getChildAt(i);
        if(child instanceof EditText){
            try{
                Field selectorWheelPaintField = np.getClass().getDeclaredField("mSelectorWheelPaint");
                selectorWheelPaintField.setAccessible(true);
                ((EditText) child).setTextColor(mContext.getResources().getColor(colorRes));
                np.performClick();
            }
            catch(NoSuchFieldException e){
            }
            catch(IllegalArgumentException e){
            }
        }
    }
}
youngwoon
  • 113
  • 1
  • 11
  • Hi, may you explain your answer better? What is currentTouchAction in the code? – MMG Sep 21 '20 at 02:48
  • @MMG Hi, currentTouchAction is to show what touch action the user has taken now. And I used it to find out when the users completed the drag. – youngwoon Sep 21 '20 at 05:06
  • @MMG And if you do like that code, the selected item's color would be changed when user completed dragging. – youngwoon Sep 21 '20 at 05:15
  • So, it is a variable. Please put more code as answer. I don't know where I should put the code. – MMG Sep 21 '20 at 09:38
  • @MMG ah, yeah, it is a variable... I was using python now a days, so I thought it was variable naturally. sorry. You can just use setOnTouchListener() at the numberpicker object! If you can't do that, can you tell me more about your problem? – youngwoon Sep 21 '20 at 14:02
  • Please attach complete code so that it shows a numberpicker and we can change its selected field color – MMG Sep 22 '20 at 09:17
  • @MMG Did you initialize "NumberPicker numberpicker"? It works well if you copy my code and use it... – youngwoon Sep 22 '20 at 13:29
  • For example in one activity put your code in correct order please, I will test it and if it works you will get the bounty. – MMG Sep 22 '20 at 22:47
  • @MMG sorry... I can't understatnd why you can't use that code...T_T What you need to do is initializing NumberPicker obejct. – youngwoon Sep 24 '20 at 23:44
  • OK, please put a code that it completely works. Do initializing and any other necessary thing in it. Of course do it if you want the bounty. @youngwoon – MMG Sep 25 '20 at 18:34
  • In image that you put in your answer, if you scroll the color of selected field will change?! – MMG Sep 28 '20 at 09:37
  • My code is here: https://stackoverflow.com/q/64086987/12478830 Please check it to see what is wrong in my code. – MMG Sep 28 '20 at 09:39
2

I think you can use a NumberPicker.OnValueChangeListener using the method setOnValueChangedListener(...)of the widget.

In the callback you get the picker and can change the style the way you want.

ESoares
  • 196
  • 6
  • Please attach complete code so that it shows a numberpicker and we can change its selected field color – MMG Sep 22 '20 at 09:17
2

You can try using below code. Just pass only your NumberPicker and color in this parameters. Hope this solves your issue

import java.lang.reflect.Field;

public static void changeNumberPickerTextColor(NumberPicker numberPicker, int color)
{

    try{
        Field selectorWheelPaintField = numberPicker.getClass()
            .getDeclaredField("mSelectorWheelPaint");
        selectorWheelPaintField.setAccessible(true);
        ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
    }
    catch(NoSuchFieldException e){
        Log.w("setNumberPickerTextColor", e);
    }
    catch(IllegalAccessException e){
        Log.w("setNumberPickerTextColor", e);
    }
    catch(IllegalArgumentException e){
        Log.w("setNumberPickerTextColor", e);
    }

    final int count = numberPicker.getChildCount();
    for(int i = 0; i < count; i++){
        View child = numberPicker.getChildAt(i);
        if(child instanceof EditText)
            ((EditText)child).setTextColor(color);
    }
    numberPicker.invalidate();  
}
MashukKhan
  • 1,516
  • 1
  • 23
  • 37
  • Your answer is very similar with https://stackoverflow.com/a/22962195/12478830 and I have tried that answer. The problem of that answer is when we click or scroll on number picker, color will be changed and it will come back to default color. – MMG Sep 25 '20 at 19:12
2

By design is not possible to change only the center color. If you check the NumberPicker's source code, specifically in the onDraw method, then you will see that the widget makes no difference between the center item and the rest of the items when rendering. All the texts are rendered using the same color.

Source code link. Check the onDraw method starting at the comment "//draw the selector wheel": https://android.googlesource.com/platform/frameworks/base/+/0e2d281/core/java/android/widget/NumberPicker.java

In addition, when editing, it places an EditText control on top of the NumberPicker center area. Changing such EditText control will not solve the problem, even if you perform some tricks such as doing a programmatic click to set the EditText into focus, because as soon as you scroll again or set the focus to a different View in your layout, the EditText will be hidden, and you will loose such color effect.

The only solution is to create a new NumberPicker control. For example you could use the actual NumberPicker source code, and adapt it to your needs. You will need to make the adjustments in the onDraw method.

PerracoLabs
  • 12,362
  • 12
  • 57
  • 107