3

I want the values in number picker to be white. I did the folloowing code which made it default black.

XML code:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@layout/gradient_blue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="25sp"
        android:textColor="#fff"
        android:textStyle="bold"
        android:text="Please select difficulty level"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@drawable/category_seperator"/>

    <LinearLayout
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/numberPicker1"
            android:layout_width="wrap_content"
            android:minWidth="100dp"
            android:layout_margin="10dp"

            android:layout_height="wrap_content"/>
            <!--#019687-->
        <Button
            android:background="#019687"

            android:id="@+id/setting_confirm"
            android:layout_width="wrap_content"
            android:layout_margin="10dp"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textColor="#fff"
            android:padding="15dp"
            android:text="OK"/>


    </LinearLayout>

</LinearLayout>

Javacode:

    final Dialog d = new Dialog(context);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);

d.setContentView(R.layout.dialog_settings);

final NumberPicker n = (NumberPicker) d.findViewById(R.id.numberPicker1);

String easy = Html.fromHtml("<font color=\"white\">EASY</font>").toString();
String medium = Html.fromHtml("<font color=\"white\">MEDIUM</font>")+"";
String hard = Html.fromHtml("<font color=\"white\">HARD</font>")+"";
String tough = Html.fromHtml("<font color=\"white\">TOUGH</font>")+"";

//        String[] a = {"EASY", "MEDIUM", "HARD", "TOUGH"};
String [ ] a = {easy,medium,hard,tough};
n.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
n.setDisplayedValues(a);


n.setMaxValue(3);
n.setMinValue(0);
n.setWrapSelectorWheel(true);

d.show();

I tried to change colour using html to as shown in code. I also tried to change the colour using getChildAt and casting it to TextView which crashed the app.

Number picker text colour

A_rmas
  • 705
  • 9
  • 25
  • 1
    Did you see this? http://stackoverflow.com/questions/22962075/change-the-text-color-of-numberpicker – Eric B. Nov 11 '15 at 07:36
  • Can you post your crash logs, because i use this exact same method and it works. – Eric B. Nov 11 '15 at 07:44
  • It didn't crashed actually got confused with some other answer but neither worked for me completes the try block shows log just before return statement yet the colour doesnot seem to change used the method as _setNumberPickerTextColor(n,R.color.white);_ – A_rmas Nov 11 '15 at 07:56
  • 1
    This is because you don't pass the resource color in this method. You pass the `Color` class's color. For Example, `Color.RED` – Eric B. Nov 11 '15 at 08:06
  • Thanks @eric247 that really worked out :D – A_rmas Nov 11 '15 at 08:10
  • 1
    Shall i post it as answer? – Eric B. Nov 11 '15 at 08:14
  • yes sure I shall tick your answer :) and plz upvote my quest :P – A_rmas Nov 11 '15 at 08:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94785/discussion-between-armas-cyndromic-and-eric247). – A_rmas Nov 11 '15 at 08:20

3 Answers3

4

As per our discussion in comments, the code here, requires a parameter from the Color class. Pass a color using this class.

Community
  • 1
  • 1
Eric B.
  • 4,412
  • 2
  • 13
  • 32
3

Just use in your style.xml file textColorPrimary atribute:

<style name="Theme.MyTheme" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    <item name="android:textColorPrimary">#FFFFFF</item>

1

copied: Change the text color of NumberPicker

The color parameter should be from Color class e.g Color.WHITE

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;
}
Community
  • 1
  • 1
A_rmas
  • 705
  • 9
  • 25