How to customize a Spinner in Android
Create a custom adapter with a custom layout for your spinner.
Spinner spinner = (Spinner) findViewById(R.id.pioedittxt5);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.travelreasons, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
R.layout.simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
R.layout.simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />
In styles add your custom dimensions and height as per your requirement.
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
</style>
<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
</style>
Style android spinner
Remove the SpinnerStyle
background attribute.
Remove this:
<style name="spinnerStyle">
<item name="android:background">@drawable/whitish_rectangle</item>
</style>
which is what draws the background white rectangle.
Building on the code in your question, here is a basic example on how you can style your Spinner:
The styles.xml
file sets styles for the SpinnerItem
and SpinnerDropDownItem
:
<resources>
<style name="customtheme" parent="@android:style/Theme.Light">
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item>
</style>
<style name="SpinnerItem">
<item name="android:textColor">#993399</item>
<item name="android:background">@drawable/my_rectangle</item>
</style>
<style name="SpinnerDropDownItem">
<item name="android:textColor">#993399</item>
<item name="android:background">@drawable/my_rectangle</item>
</style>
</resources>
For testing, I've created a bright-colored drawable shape, called my_rectangle.xml
. Replace this with your own drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ff0000" />
<stroke
android:width="1dp"
android:color="#888888" />
</shape>
Add the Spinner
to the Activity's
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dip">
<Spinner
android:id="@+id/edit_countrySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/location_names"/>
</LinearLayout>
This produces:
with no white square in the background.
Spinner color style in Android
By using below code I can solve my problem.
<Spinner
android:id="@+id/static_spinner2"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:layout_marginLeft="30dp"
android:background="@drawable/apptheme_spinner_background_holo_light"
android:popupBackground="#ffffff" />
Spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:background="#FFFFFF"
android:textColor="#000000" />
spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#000000"/>
java code :
Spinner staticSpinner = (Spinner)findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter =
ArrayAdapter.createFromResource(this,R.array.request_role,R.layout.spinner_item);
staticAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);
Is there any easy way to change Spinner dropdown color in Android?
yes. You can use following attribute fro spinner inside your xml
android:popupBackground="YOUR_HEX_COLOR_CODE"
to change textcolor etc Make a custom XML file for your spinner item.
spin_item.xml:
Then provide it your desired color and sizes :
<?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="24sp"
android:textColor="#000000"
android:padding="4dp"
/>
And then use it like this:
val adapter = ArrayAdapter.createFromResource(activity,
R.array.email_type_array, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(R.layout.spin_item)
How do I change the text style of a spinner?
When you create the Adapter that backs the Spinner you can set a layout for the spinner item.
spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view));
You can style some_text_view the way you want.
<TextView android:id="@+id/some_text_view" android:textStyle="bold" />
Related Topics
What Happens If a Android Service Is Started Multiple Times
Data Directory Has No Read/Write Permission in Android
Asynctask, Must It Take Such a Performance Penalty Hit...
5.1 Crash - a Taskdescription's Primary Color Should Be Opaque
Pass Parcelable Argument with Compose Navigation
Android Imageview's Onclicklistener Does Not Work
How to Show/Hide Password in Textformfield
Android: Outofmemoryerror: Bitmap Size Exceeds Vm Budget with No Reason I Can See
Android Cardview Remove Padding
Fragmentactivity Cannot Be Resolved to a Type
How to Add Parameters to API (Http Post) Using Okhttp Library in Android
How to Get Chrome Browsing History/Bookmarks in Our Android App
Android:Dither="True" Does Not Dither, What's Wrong
How to Move an Image from Left to Right in Android
Opening Infowindow Automatically When Adding Marker Google Maps V2 Android