Change text color of selected item in spinner
drawable/mybg:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<color android:color="@color/black" />
</item>
</layer-list>
This will change the selected item color in the popup.
Change text color to selected item on Android Spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView)parent.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
Change the TEXT (not background) color of a spinner when an item is selected
if the user selects an option (causing it to become what is displayed
on top), I would like that text to become red.
So you most likely created OnItemSelectedListener() for your Spinner. So in onItemSelected() method you can simply change text color.
Pseudocode:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.RED);
}
}
Hope it helps.
How to change the color of the selected item of a spinner
You have to try like this
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.RED);
}
}
How to set the selected item color in the spinner?
You need to create a custom spinner layout to achieve what you want.
check out these questions, they have the answers you want:
How to customize a Spinner in Android
Android: Custom Spinner Layout
The idea is to create a layout for your row, and set it when creating a spinner with its adapter in code.
android change text color of items in spinner
I figured out that to make this work you have to override the getDropDownView when setting up the ArrayAdapter in the main activity.
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PatchedSpinner pSpinner = (PatchedSpinner) findViewById(R.id.spinner2);
ArrayList<String> testarray = new ArrayList<String>();
testarray.add("item0");
testarray.add("item1");
testarray.add("item2");
testarray.add("item3");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, testarray) {
@Override
public boolean isEnabled(int position) {
return position != 1;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView tv = (TextView) v.findViewById(R.id.spinnerTarget);
tv.setText(testarray.get(position));
switch (position) {
case 0:
tv.setTextColor(Color.RED);
break;
case 1:
tv.setTextColor(Color.BLUE);
break;
default:
tv.setTextColor(Color.BLACK);
break;
}
return v;
}
};
pSpinner.setAdapter(spinnerAdapter);
}
The layout that is being inflated is a custom layout called row.xml. it is used to set the display for the dropdown view.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14pt" />
Android Change Text Color of Spinner (displayed text - not items in spinner)
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int index = adapterView.getSelectedItemPosition();
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.white));
}
Android : Change the text color of a spinner item inside the dropdown list
You can reach this goal setting up an adapter. You should create a custom adapter showing your items in a simple way.
this is the official reference for adapters. You can refer to this for any method or variable.
this SO link has the solution.
So what you have to do is:
- Create a custom adapter
- Create the layout and the java code for the adapter (you can easily find on google thousands of examples
- Add the code from this (you will need to customize it for your needs.
ADAPTER
class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {
private int mSelectedIndex = -1;
public void setSelection(int position) {
mSelectedIndex = position;
notifyDataSetChanged();
}
public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
super(context, resource, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View itemView = super.getDropDownView(position, convertView, parent);
if (position == mSelectedIndex) {
itemView.setBackgroundColor(Color.rgb(56,184,226));
} else {
itemView.setBackgroundColor(Color.TRANSPARENT);
}
return itemView;
}
}
PS:
Since the setSelection
method is not native, you need to call it from the OnItemSelectedListener
in the activity.
Hope this helps.
Related Topics
How to Create Always-Top Fullscreen Overlay Activity in Android
How to Run Opencv Code Without Opencv Manager
How to Set Spinner Default Value to Null
How to Show Progress Dialog in Android
How to Execute a Method by Clicking a Notification
Inserting Contacts in Android 2.2
Anr Keydispatchingtimedout Error
Receive Mms Messages in Android Kitkat
Broadcast Receiver to Detect Application Start
Why Is the Android Test Runner Reporting "Empty Test Suite"
What Are the Differences Among Internal Storage, External Storage, Sd Card and Removable Storage
How to Know When Tts Is Finished
Why Is Android Webview Refusing User Input
Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra