NumberPicker textColour

ClaireG picture ClaireG · Aug 8, 2013 · Viewed 8.6k times · Source

I have a ListView containing a TextView and a NumberPicker for each row. Whenever I start up the activity, the numbers of the NumberPicker are white (the same color as the background).

I tried applying a style as suggested here, here, and here ; to no avail.

Any other suggestions?

-

NumberPicker.xml

<NumberPicker 
    android:id="@+id/npMaterialAmount"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="75dp"
    style="@style/npColour"/>

-

Tried style 1:

<style name = "npColour" >
    <item name = "android:textColor">#000000</item>
</style>

-

Tried style 2:

<style name = "npColour" parent ="@android:style/Theme.Holo.Light">
</style>

-

... and the list goes on...

Answer

phil picture phil · Mar 13, 2015

I know the question is old. I founded the answer here: Change the text color of NumberPicker

Copy:

This code should solve your problem. The problem you are experiencing is because during the construction of NumberPicker it captures the EditText textColor and assigns to to a paint so it can draw the numbers above and below the edit text with the same color.

import java.lang.reflect.Field;

public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color)
{
    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();
                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;
}