Change the text color of NumberPicker

android number picker text size
change line color of numberpicker android
number picker android
colorcontrolnormal numberpicker
horizontal number picker android
scrollable number picker android
android number picker formatter
android numberpicker accessibility

I've looked at most all of the threads on this and none provided an answer that works. Styling the NumberPicker does not work (as per this thread: NumberPicker textColour)

Setting the style attribute on the numberPicker to a style that has a color item does not have any effect either. Nor does setting the textColor attribute on the numberPicker XML do anything.

Closest I've got to this is using the numberPicker to cast its getChildAt() to an EditText and then do setColor() on that EditText, but that only changes the color of the child once upon initialization and then every time it is selected from thereon; not what I am looking for either.

Any help? Thanks

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 void setNumberPickerTextColor(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();  
}

How to change NumberPicker Text Color? (White on White , How to change NumberPicker Text Color? (White on White background). Hey there,. My app uses the Holo Light Style, and I open (Light) a Dialog  Change NumberPicker TextSize. Ask Question Asked 4 years, 8 months ago. Change the text color of NumberPicker. 516. Android changing Floating Action Button color.

The solution I tried and worked for me is:

In styles.xml add:

<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:textColorPrimary">@android:color/black</item>
</style>

Then use it like this inside your layout:

  <NumberPicker
    android:id="@+id/dialogPicker"
    android:theme="@style/AppTheme.Picker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp" />

Change the text color of NumberPicker, Setting the style attribute on the numberPicker to a style that has a color item does not have any effect either. Nor does setting the textColor attribute on the  Use theming to override the number picker's divider color in API 21+: ?attr/colorControlNormal determines the color of the divider in material number picker, so changing this color in your widget's theme will do the trick, e.g. for DatePicker:

Not sure why you would need to dive into Java Reflection API for this. Its a simple styling matter. The attribute that you need to override is: textColorPrimary.

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    ....
    <item name="android:textColorPrimary">#ff0000</item>
</style>

If you're using the TimePicker inside a Dialog, override android:textColorPrimary in the dialog's theme.

That's about it.

Additional info:

Here's an insightful comment by Yoann Hercouet:

This solution does not change only the color on the NumberPicker, it is a global change that will impact A LOT of components

This is correct, but it overlooks the possibilities I am hinting at. Moreover, global implies app-wide impact. That can be limited to activity-scope by applying this theme only to activities containing the NumberPicker. But, I agree, this may still be too corrosive.

The idea here is to somehow inject textColorPrimary=INTENDED_COLOR into the theme that will be seen by NumberPicker. There are multiple ways to achieve this. Here's one way:

Define a bare-bone style in res/values/styles.xml:

<style name="NumberPickerTextColorStyle">
    <item name="android:textColorPrimary">@color/intended_color</item>
</style>

Now, create a custom NumberPicker:

public class ThemedNumberPicker extends NumberPicker {

    public ThemedNumberPicker(Context context) {
        this(context, null);
    }

    public ThemedNumberPicker(Context context, AttributeSet attrs) {
        // wrap the current context in the style we defined before
        super(new ContextThemeWrapper(context, R.style.NumberPickerTextColorStyle), attrs);
    }
}

Finally, use ThemedNumberPicker in your layout(s):

<package.name.ThemedNumberPicker
    android:id="@+id/numberPicker"
    ....
    ....
    .... />

We have successfully contained the impact that textColorPrimary=INTENDED_COLOR has on our app.

This is of course just one option. For example, if you were inflating a layout containing a NumberPicker, you could use:

// In this case, the layout contains <NumberPicker... />, not <ThemedNumberPicker... />
LayoutInflater.from(new ContextThemeWrapper(context, R.style.NumberPickerTextColorStyle))
    .inflate(R.layout.number_picker_layout, ...);

Android NumberPicker change text color, Would there be a way to customize the text color in my NumberPicker? Also, I know it is a different question, but the colors of the top and bottom 'bar' as well  Change the text color of NumberPicker (5) Here is a Xamarin Snippet from the answer above with TextSize and TextStyle Bold

Here is a Xamarin Snippet from the answer above with TextSize and TextStyle Bold

public static bool SetNumberPickerTextColorAndSize(NumberPicker numberPicker, Color color, ComplexUnitType complexUnitType, float textSize, TypefaceStyle style)
    {
        int count = numberPicker.ChildCount;
        for (int i = 0; i < count; i++)
        {
            View child = numberPicker.GetChildAt(i);
            if (child.GetType() == typeof(EditText))
            {
                try
                {
                    Field selectorWheelPaintField = numberPicker.Class
                                                                .GetDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.Accessible = true;

                    EditText editText = (EditText) child;
                    editText.SetTextSize(complexUnitType, textSize);
                    editText.SetTypeface(editText.Typeface, style);
                    editText.SetTextColor(color);

                    Paint paint = (Paint) selectorWheelPaintField.Get(numberPicker);
                    paint.TextSize =  TypedValue.ApplyDimension(complexUnitType, textSize, numberPicker.Resources.DisplayMetrics);
                    paint.Color = color;
                    paint.SetTypeface(editText.Typeface);

                    numberPicker.Invalidate();
                    return true;
                }
                catch (NoSuchFieldException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
                catch (IllegalAccessException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
                catch (IllegalArgumentException e)
                {
                    Log.Warn("setNumberPickerTextColor", e);
                }
            }
        }
        return false;
    }

Change text color of Number picker., Hello, first of all thanks for this wonderful app!! Now I'm doing a scene using a couple of pickers but I have a problem with the text color of the  What I am hoping to achieve is change the color of the current text showing in a number picker. The following line of code placed in the onCreate has this desired affect but the color is lost when scrolled. ((EditText)picker.getChildAt(0)).setTextColor(Color.RED); Please include the stack trace from the Logcat view.

Instead of changing every text color to the color you want, better just changing all editText color. NumberPicker actually has a child EditText that display the numbers.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Change edit color here. -->
        <item name="android:editTextColor">#000000</item>
</style>

This worked for me. And although I have white text in the buttons, they havent changed.

android.widget.NumberPicker.getChildAt java code examples, Change the text color of NumberPicker. View child = numberPicker.getChildAt(i); if(child instanceof EditText){ try{. origin: stackoverflow.com  Thanks for the answers! but I couldn't change text color and font of the elements in the picker for iOS. Only background color is changed. Is there anything I should add to here? `protected override void OnElementChanged(ElementChangedEventArgs e) {base.OnElementChanged(e);

NumberPicker child text color, Think I might just set the color of the divider rather than the text as the number picker is changed. Got this working. Click to expand Click for  The thing is that the color of the number picker seams to change depending of the device (or Android version). I think for now, I will have to use the scene in action mode. You received this message because you are subscribed to the Google Groups "Tasker" group. To unsubscribe from this group and stop receiving emails from it,

core/java/android/widget/NumberPicker.java, Color;. import android.graphics.Paint;. import android.graphics.Paint.Align; The height of the gap between text elements if the selector wheel. */. private int Callback invoked while the number picker scroll state has changed. *. * @param​  Text Color. The color property is used to set the color of the text. The color is specified by: a color name - like "red" a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" Look at CSS Color Values for a complete list of possible color values. The default text color for a page is defined in the body selector.

ANdroid NumberPicker color, I quickly changed the text color of the text view by applying a style, but I cannot in any way find how to change the number colors of the  Overview; Android Platform; Android Support Library; AndroidX; AndroidX Test; AndroidX Constraint Layout; Architecture Components; Jetpack Compose UI; Android Automotive Library

Comments
  • This worked! even with net.simonvt.numberpicker ! It made me crazy that no matter what style I specified, I kept ending up with black text! (black text on black background = useless)
  • Worked for me. You saved me from jumping off my apartment.
  • All you need to do is override textColorPrimary in your theme. Calling on Reflection API is unnecessary in this case.
  • Agreed. However it must be said that this also changes the text color in your entire app and not just the date time picker which may not suite. The question is specific to NumberPicker.
  • I don't like to downvote, and this answer shows good effort, and works, but it is so much worse than @Vikram's answer below that I am downvoting, because I think the community will be better served by Vikram's answer.
  • This is the simplest and best answer overall. +1
  • Definately the simplest solution! This is also easy to combine with my custom numberpicker, for which used the theme @android:style/Theme.Dialog to show + and - buttons instead of the slider effect. Now I simply defined the parent as my previous theme (instead of Theme.AppCompat.Light.NoActionBar), and it magically works combined.
  • This is the best answer. I wish I could up-vote more then +1.
  • @z3n105 do you know the property for changing the selection color?
  • A much better approach.
  • @Milad If you can give/post some more details about your app setup, I can probably help you out.
  • doesn't work on me either. But using this method worked: stackoverflow.com/questions/13882707/…
  • @HenriquedeSousa Like I said to user Milad - if you can post your style setup, I can probably help you out. Moreover, the answer you linked to doesn't make sense (unfortunately). See my comment on that answer.