Reputation: 37
In the above picture GAIN 3 is selected but its not visible properly , so how can i change that color to darker color. basically i want to change the selected text background in darker color.
I'm using com.jaredrummler.materialspinner.MaterialSpinner
Spinner.
Here's the java implementation.
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
text = spinner.getText().toString();
Log.e("Spinner Listener",text);
if(text.contains("GAIN 0")){
sendToDevice("F");
} else if(text.contains("GAIN 1")){
sendToDevice("G");
} else if(text.contains("GAIN 2")){
sendToDevice("H");
} else if(text.contains("GAIN 3")){
sendToDevice("I");
}
}
});
And the layout item looks like the following.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinner"
app:ms_dropdown_max_height="350dp"
app:ms_dropdown_height="wrap_content"
android:textColorHighlight="#000000"
android:layout_width="130dp"
style="@style/spinner_style"
android:popupTheme="@android:style/ThemeOverlay.Material"
android:textColor="@color/blue"
android:layout_below="@+id/testmodetitle"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toEndOf="@+id/button1"
android:layout_marginStart="30dp" />
Upvotes: 1
Views: 3122
Reputation: 31
Give html colour code for the first item of spinner.
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText),
TextView.BufferType.SPANNABLE);
Upvotes: 0
Reputation: 540
To change background color and other color this library has provided some attributes. To change background color of selected item use below code.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_background_selector="@drawable/selector_gray_white_spinner"
app:ms_dropdown_height="wrap_content"
app:ms_dropdown_max_height="350dp" />
create one selector in drawable having name selector_gray_white_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/darkGray"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/darkGray"/>
<item android:state_focused="true" android:drawable="@android:color/white"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/white"/>
</selector>
Add dark color in your color.xml file
<color name="darkGray">#acacac</color>
Upvotes: 1
Reputation: 24211
There are some attributes available along with the implementation of that specific library. Please have a look in the readme.md section where the attributes are listed.
I think you might consider using ms_background_selector
attribute in your layout where you have declared the spinner.
So the layout declaration will look like this.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinner"
app:ms_background_selector="@drawable/your_darker_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Add a file named your_darker_selector.xml
and put the following code inside the file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@android:color/darker_gray"/>
<item android:state_checked="false" android:drawable="@android:color/white" />
</selector>
Modify the color from the selector file as per your necessity.
Upvotes: 1
Reputation: 444
Use this way it will help you:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
v = super.getDropDownView(position, null, parent);
// If this is the selected item position
if (position == selectedItem) {
v.setBackgroundColor(Color.BLUE);
}
else {
// for other views
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
Upvotes: 1