In my Android application, I am using spinner, and I have loaded data from the SQLite database into the spinner, and it\'s working properly. Here is the code for that.
If you want a simple method, in order to add items to a dropdown, you usually add them to the strings.xml. Here is an example on how to add colour by using the strings.xml file:
SELECT AGE RANGE
<string-array name="age_array">
<item> 0-6 </item> //No custom colour
<item><font fgcolor='#FF4CD964'> 12+ </font></item> //With custom colour
</string-array>
we can change style of spinner textview set theme for spinner like this:
styles.xml:
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textSize">@dimen/_11ssp</item>
<item name="android:textColor">@color/blue</item>
<item name=...</item>
</style>
then
<Spinner
android:theme="@style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
if you want to change spinner textview attribute programtically:
Programatically:
val textView = (view.getChildAt(0) as TextView)
textView.setTextColor(resources.getColor(R.color.dark_mode))
String typeroutes[] = {"Select","Direct","Non Stop"};
Spinner typeroute;
typeroute = view.findViewById(R.id.typeroute);
final ArrayAdapter<String> arrayAdapter5 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, typeroutes) {
@Override
public boolean isEnabled(int position) {
if (position == 0) {
// Disable the first item from Spinner
// First item will be use for hint
return false;
} else {
return true;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
((TextView) v).setTextColor(Color.parseColor("#ffffff"));
return v;
} ---->in this line very important so add this
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.BLACK);
}
return view;
}
};
arrayAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeroute.setAdapter(arrayAdapter5);
that's all enjoy your coding...
Simple and crisp...:
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
I have done this as following.I have use getDropDownView() and getView() methods.
Use getDropDownView()
for opened Spinner.
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item));
}
return view;
}
And Use getView()
for closed Spinner.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(R.color.context_item_disabled));
return view;
}
To prevent lagging, you need to not only set the text properties in the onItemSelected
listener, but also in the Activity's onCreate
method (but it's a little tricky).
Specifically, you need to put this in onCreate
after setting the adapter:
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
And then put this in onItemSelected
:
((TextView) view).setTextColor(backgroundColor);
Here is a full example:
@Override
protected void onCreate(Bundle savedInstanceState)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Set the choices on the spinner by setting the adapter.
spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));
//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);
//Set the listener for when each option is clicked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Change the selected item's text color
((TextView) view).setTextColor(backgroundColor);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
For more details, see my question.