Reputation: 95
I am trying to change the color of the text and the button for the Spinner function programatically. how can i change the text and button color of my spinner without changes in the xml??
Following code:
Spinner label_Col_5_dataSpinner = new Spinner(this);
label_Col_5_dataSpinner.setId(200+Count);
String[] label_Col_5_data= new String[]{"Vivian","Xiaomi","Chris","Bill","Luyi"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,label_Col_5_data );
label_Col_5_dataSpinner.setAdapter(adapter);
label_Col_5_dataSpinner.setPadding(30, 20, 30, 20);
label_Col_5_dataSpinner.setBackgroundColor(Color.TRANSPARENT);
//label_Col_5_dataSpinner.setTextColor(Color.BLACK);
tr.addView(label_Col_5_dataSpinner);
Upvotes: 0
Views: 1874
Reputation: 1838
For text color hopefully this will work:
Spinner spinner = (Spinner)findViewById(R.id.my_spinner);
TextView tv = (TextView) spinner.getSelectedView();
tv.setTextColor(Color.BLACK);
The best and easiest solution to change the button color:
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
Other solution if you don't want change all Spinners:
Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();
spinnerDrawable.setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spinner.setBackground(spinnerDrawable);
}else{
spinner.setBackgroundDrawable(spinnerDrawable);
}
For more information check out: How to change the Text color of Spinner (for text color)
&
Change colour of small triangle on spinner in android (for button color)
Hope it helps! Good Luck!
Upvotes: 0
Reputation: 271
for change button color you can use this :
Drawable spinnerDrawable = mySpinner.getBackground().getConstantState().newDrawable();
spinnerDrawable.setColorFilter(getResources().getColor(R.color.blue_m), PorterDuff.Mode.SRC_ATOP);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{ mySpinner.setBackground(spinnerDrawable); }
else
{ mySpinner.setBackgroundDrawable(spinnerDrawable); }
Upvotes: 3
Reputation: 432
Better to use the :
Button >> on click open dialog
OR
AutoCompleteTextView >> as u start typing it will show as dropdown list
Upvotes: 0
Reputation: 42844
If you are using xml .... Since its easy and powerful
Styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- Remove shadow below actionbar -->
<item name="android:windowContentOverlay">@null</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<!-- set background for entire application -->
<item name="android:windowBackground">@color/app_background</item>
<!-- For the resting Spinner style -->
<item name="android:spinnerItemStyle">
@style/spinnerItemStyle
</item>
<!-- For each individual Spinner list item once clicked on -->
<item name="android:spinnerDropDownItemStyle">
@style/spinnerDropDownItemStyle
</item>
</style>
<!-- Spinner Custom color -->
<style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<!-- <item name="android:padding">10dp</item> -->
<item name="android:textSize">12sp</item>
<item name="android:textColor">#00aeef</item>
</style>
<style name="spinnerDropDownItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<!-- <item name="android:padding">20dp</item> -->
<item name="android:textSize">12sp</item>
<item name="android:textColor">#00aeef</item>
</style>
</resources>
Upvotes: 0
Reputation: 119
You can use this way -
spinner.setOnItemSelectedListener(OnCatSpinnerCL);
and define this as -
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextSize(getResources()
.getDimension(R.dimen.detail_spinner_text_size));
// Change the color here as well
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
Hope this helps :)
Upvotes: 0