Skip to content Skip to sidebar Skip to footer

How Can I Change Color Of Selected Item In Number Picker And Spinner

i want to change the colour of selected text in spinner. is there anyway i can change boundaries to white as well . same problem with number picker i want to appear color of number

Solution 1:

You can do this in the layout for your Adapter with a ColorStateList. You can create a ColorStateList using xml in your color resource folder like so:

<?xml version "1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android/com/apk/res/android"><itemandroid:state_checked="true"android:color="?attr/colorAccent"/><itemandroid:state_selected="true"android:color="?attr/colorAccent"/><itemandroid:state_pressed="true"android:color="?attr/colorAccent"/><itemandroid:state_activated="true"android:color="?attr/colorAccent"/><itemandroid:color="?android:attr/textColorPrimary"/></selector>

Then you can create a layout using the ColorStateList (I use it here for the text color, but you can set any attribute that can use a color resource):

<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"android:textSize="16sp"android:textColor="@color/selector_accent"/>

Lastly, set this layout as your drop down view resource:

dataAdapter.setDropDownViewResource(R.layout.view_selectable);

As for the NumberPicker, it doesn't look like there is a standard way to set the layout, but you should be able to change the color of the text, based on this answer. Then just set the text color using your ColorStateList:

editText.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_accent));

Post a Comment for "How Can I Change Color Of Selected Item In Number Picker And Spinner"