Changing the Color of a Spinner - android

I have a spinner that I want to change the color when it is not clicked. the view in the activity. I managed to change the color of text and background when it is clicked, but not the default text and background. and because the background of the app is dark it is difficult to see. How can I do this. this is my spinner code on activity_main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="114dp"
android:layout_height="33dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:theme="#style/Spinner"
app:layout_constraintBottom_toTopOf="#+id/ping_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toEndOf="#+id/editText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.025" />
my style for spinner
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/textColor</item>
<item name="android:backgroundTint">#color/background</item>
<item name="android:textSize">14sp</item>
</style>
here's a screenshot of the app
http://pctechtips.org/apps/droid.png

Make a custom XML file for your spinner item.
spinner_item.xml:
Give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.
Reference from 'How to change spinner text size and text color?'

Related

Setting spinner popup background color

I can set the background of the popup menu of a spinner using:
<Spinner
android:popupBackground="#color/darkThemeBackgroundColor"/>
But how do I set the background text color of it?
This solution is more simpler, you only need add this code to styles.xml
<style name="spinnerTheme">
<item name="android:background">#color/colorDark</item>
<item name="android:textColor">#android:color/white</item>
<!-- this is for api 19 text color -->
<item name="android:color">#android:color/white</item>
</style>
And this is how use in your layout file
<Spinner
android:id="#+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:entries="#array/aves"
android:theme="#style/spinnerTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
That is the result:
I hope this solution helps you, greetings.

Customize the Spinner Dialog with transparent background color and has divider between its items

I have successfully designed a dialog spinner as shown below. Now what I want is to set the background color of the spinner to be transparent and add the divider between the items.
What I have done is adding a "Spinner Style" style of the spinner in styles.xml with android:divider, android:height divider and android:padding attributes, and then add the style attribute to the Spinner, however, the result is no different.
On the other hand, I have tried to merely change the color of the spinner pop up a background to be transparent, however, this result is not my desired one.
Hence, I hope to see any advice and documents to solve this problem! Thanks for all help!
my current spinner pop up and what I want could look like desired result(the autocomplete view is irrelevant; I just care about the background and divider between the items)
**customize_spinner_shape_in_search_frame.xml**
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
style="#style/spinnerStyle">
<!-- view background color -->
<solid
android:color="#EFB1B9" >
</solid>
<corners
android:radius="20dp" >
</corners>
</shape>
**Spinner in layout.xml**
<Spinner
android:id="#+id/search_criteria_activity_spn_subject_title"
android:layout_width="188dp"
android:layout_height="51dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:spinnerMode="dialog"
android:backgroundTintMode="add"
android:background="#drawable/customize_spinner_shape_in_search_frame"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_criteria_activity_s2"
style="#style/spinnerStyle"/>
**And lastly, the SpinnerStyle in styles.xml:**
<style name="spinnerStyle" parent="Widget.AppCompat.Spinner">
<item name="android:divider">#d1d1d1</item>
<item name="android:dividerHeight">0.5dp</item>
<item name="android:padding">5sp</item>
</style>
You can use android:popupBackground="#android:color/transparent" to change Popup background to transparent
<Spinner
android:id="#+id/search_criteria_activity_spn_subject_title"
android:layout_width="188dp"
android:layout_height="51dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:spinnerMode="dialog"
android:backgroundTintMode="add"
android:background="#drawable/customize_spinner_shape_in_search_frame"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_criteria_activity_s2"
style="#style/spinnerStyle"
android:popupBackground="#android:color/transparent"/>

android spinner dropdown custom

I use spinner to show something, this is my code :
ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(), android.R.layout.simple_spinner_dropdown_item, nameList);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
with this code, when click on spinner, I can see text and checkbox.
after that, I try to custom spinner like this :
ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(),
R.layout.list_spinner, tenfavList);
adapter.setDropDownViewResource(R.layout.list_spinner_dropdown);
this is list_spinner_dropdown.xml :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="#dimen/20sp"/>
when but with this code, when click on spinner, I just see text, and no checkbox.
how to fix it ?
OK, now i got your issue. Issue is only that you please add style in your CheckedTextView for checked or unchecked like :
Add File default_checkbox.xml inside res/drawable
<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="true"
android:drawable="#drawable/checkbox_checked" /> <!-- checked -->
<item android:state_pressed="true"
android:drawable="#drawable/checkbox_checked" /> <!-- pressed -->
<item android:drawable="#drawable/checkbox_default" /> <!-- default -->
In your Xml :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:checked="true"
android:checkMark="#drawable/default_checkbox"
android:text="sdfsdfsdfsdfdsfdsf"
android:textColor="#0004ff"
android:textSize="25dp"/>

How to change text color in spinner without using textview

I want to change text color for spinner without using textView, I have already searched and found some tutorials Android: Where is the Spinner widget's text color attribute hiding?
but the main thing is they have used textView.
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:entries="#array/Gender_Selection_arrays"
android:prompt="#string/Gender_Selection"
android:id="#+id/gendersel"
android:popupBackground="#67656c"/>
I know this code is not to change text color.
I have no idea how to do that,please guide me for doing the same.
Any help would be appreciable.
I got solution for this, Create a new xml "spinner_style.xml"
<?xml version="1.0" encoding="UTF-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/Black" />
And fix this layout as your spinner style in your activity:
ArrayAdapter<String > gender_adapter = new ArrayAdapter<String> (getActivity(), R.layout.spinner_style,gender_spinner );
Your Spinner
<Spinner
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:entries="#array/Gender_Selection_arrays"
android:prompt="#string/Gender_Selection"
android:id="#+id/gendersel"
style="#style/mySpinnerItemStyle"
android:popupBackground="#67656c"/>
mySpinnerItemStyle (Add this to styles.xml)
<style name="mySpinnerItemStyle" parent="Base.Widget.AppCompat.Spinner">
<item name="android:textColor">#color/my_spinner_text_color</item>
</style>
And finally in colors.xml
<color name="my_spinner_text_color">#FFFFFF</color>
mySpinnerItemStyle inherits from Base.Widget.AppCompat.Spinner and in that android:textColor attribute changes the color of the spinner text

Style of RadioButton magically changes when put into a Listview?

I made a DialogFragment with a RadioButton ontop and a ListView below containing more RadioButtons. And now I'm just wondering what style the ListView uses that the RadioButtons don't look the same as the "stand-alone" one.
Here is a snippet of my dialog.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/buttonGroup"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="#+id/none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/white" />
<ListView
android:id="#+id/conferences"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And my list_item.xml that I inflate as the rows in getView of my ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/conference"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
My AppTheme is based on "Theme.AppCompat". I didn't change any style of a specific item in my View.
The "stand-alone" RadioButton has a white circle with a blue dot when selected and even has a different style while you press it down. The RadioButtons in the ListView are all black with a black dot and don't have a "press-down" style. It would be cool if I wouldn't need to programatically set styles for not checked/"press-down"/checked. I already tried to set 2 base Android themes that had "RadioButton" in their names on the ListView but nothing changed. Maybe there is a way to get the style of the "stand-alone" RadioButton and set it in the xml files for the other RadioButtons?
I would post an image but I'm new here so not already allowed to do this.
Update
I changed my AppTheme to this:
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:textViewStyle">#style/TextAppearance.AppCompat.Large</item>
<item name="android:radioButtonStyle">#style/RadioButtonStyle</item>
</style>
<style name="RadioButtonStyle">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Large</item>
</style>
And the RadioButtons in the ListView changed but not the "stand-alone" one. When I add the style to the RadioButton:
<RadioButton
android:id="#+id/none"
style="#style/RadioButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/none" />
the text changes but the RadioButton-circle is still there. For the RadioButtons in the ListView the circles are gone...
Make sure that LayoutInflater in your Adapter uses Activity context and not the application context.
Update:
Please remove android prefix from your style:
<item name="radioButtonStyle">#style/RadioButtonStyle</item>
And update RadioButtonStyle as follows:
<style name="RadioButtonStyle" parent="#style/Widget.AppCompat.CompoundButton.RadioButton" />

Categories

Resources