user6313669
user6313669

Reputation: 365

How can I change spinner background color?

How can I change spinner background color?

snapshot

this black color from style .

How can I change style color for spinner pop up?

I want to change background color white in place of black. How can I change?

spinnner

<Spinner
    android:id="@+id/spinner"
    android:layout_width="100dp"
    android:layout_marginTop="1dp"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner_bg"
    android:layout_marginLeft="1dp"/>
 

spinnerbg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/InputBg1" />
    </item>
    <item android:gravity="center_vertical|right" android:right="8dp">
        <layer-list>
            <item android:width="12dp" android:height="12dp" android:background="#fff" android:gravity="center" android:bottom="10dp">
                <rotate
                    android:fromDegrees="45"
                    android:toDegrees="45">
                    <shape android:shape="rectangle">
                        <solid android:color="#ffffff" />
                        <stroke android:color="#ffffff" android:width="1dp"/>
                    </shape>
                </rotate>
            </item>
            <item android:width="20dp" android:height="10dp" android:bottom="21dp" android:background="#fff" android:gravity="center">
                <shape android:shape="rectangle">
                    <solid android:color="@color/InputBg1"/>
                </shape>
            </item>
        </layer-list>
    </item>
</layer-list>

Upvotes: 3

Views: 29693

Answers (9)

Sahil Bansal
Sahil Bansal

Reputation: 729

You can wrap the spinner inside the relative layout and add background drawable to the relative layout.Code is as follows.

<RelativeLayout 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/borderbottom_white"<!-- white background with 
  bottom border -->
 android:layout_marginTop="15dp"  >
    <Spinner
    android:id="@+id/postfield_category"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:background="@null"
    android:minHeight="0dp" />
    <ImageView 
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/arrowspinner" />
</RelativeLayout>

Upvotes: 0

Vergiliy
Vergiliy

Reputation: 1356

To change the background color of the drop-down list, add android:colorBackground parameter to the theme in your styles.xml

Code:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:colorBackground">#ff0000</item>
</style>

Thus, the overall style is preserved: the effect when pressed, rounded corners, etc.

Screenshot:

enter image description here enter image description here

Upvotes: 0

Mitch
Mitch

Reputation: 1856

To change Spinner items background in an entire app, add-in styles.xml

<resources>
    <style name"AppTheme" patent="...">
        <item name="android:spinnerStyle">@style/Widget.AppTheme.Spinner</item>
    </style>

    <style name="Widget.AppTheme.Spinner" parent="Widget.AppCompat.Spinner">
        <!-- color is change form here -->
        <item name="android:popupBackground">#FFFFFF</item>
    </style>
</resources>

Upvotes: 0

Sineth Lakshitha
Sineth Lakshitha

Reputation: 713

You can simply add the following code inside <Spinner/> to change popup background.

android:popupBackground="COLOR_CODE"

Now the code should be as follow,

<Spinner
    android:id="@+id/spinner"
    android:layout_width="100dp"
    android:layout_marginTop="1dp"
    android:layout_height="wrap_content"
    android:background="@drawable/spinner_bg"
    android:layout_marginLeft="1dp"
    android:popupBackground="COLOR_CODE"
/>

Upvotes: 2

MSS
MSS

Reputation: 3633

Add the following in your styles.xml to change the background color and text color of dropdown.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Spinner style -->
    <item name="android:spinnerStyle">@style/AppTheme.spinnerStyle</item>
    <!-- Spinner dropdown style-->
     <item name="android:spinnerDropDownItemStyle">@style/AppTheme.spinnerDropDownItemStyle</item>

</style>
<style name="AppTheme.spinnerStyle" parent="@android:style/Widget.Material.Light.Spinner">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@color/colorPrimary</item>
</style>
<style name="AppTheme.spinnerDropDownItemStyle" parent="@android:style/Widget.Material.DropDownItem.Spinner">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:background">@color/colorPrimary</item>
</style>

In your AndroidManifest.xml add the theme. (Check the value of android:theme)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Upvotes: 0

user6313669
user6313669

Reputation: 365

Couple of things need to do with customization spinner as per below :-

spinner_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>

<color android:color="@color/InputBg1" />
</item>
<item android:gravity="center_vertical|right" android:right="8dp">
<layer-list>
<item android:width="12dp" android:height="12dp"  android:gravity="center" android:bottom="10dp">
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
            <stroke android:color="#ffffff" android:width="1dp"/>
        </shape>
    </rotate>
</item>
<item android:width="20dp" android:height="10dp" android:bottom="21dp" android:gravity="center">
    <shape android:shape="rectangle">
        <solid android:color="@color/InputBg1"/>
    </shape>
</item>
</layer-list>
</item>
</layer-list>

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<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:textColor="#ffffff"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

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="@style/spinnerItemStyle"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#000000"
    android:ellipsize="marquee"/>

spinner

<Spinner
android:id="@+id/spinner"
android:layout_width="100dp"
android:popupBackground="#ffffff"
android:layout_marginBottom="1dp"
android:layout_height="wrap_content"
android:textColor="#ffff"
style="@style/spinnerItemStyle"
android:background="@drawable/spinner_bg"
android:layout_marginLeft="1dp" />

style.xml

<style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">#000000</item>
</style>

Creating adapter for spinner

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

snapshot

Upvotes: 2

Murli Prajapati
Murli Prajapati

Reputation: 9713

Create style like this in folder res/values/styles.xml

<style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">Put text color code here</item>
    <item name="android:background">Put background color code here</item>
</style>  

Apply to your spinner

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/spinnerItemStyle"/>

Upvotes: 1

Shabbir Dhangot
Shabbir Dhangot

Reputation: 9121

You can set the spinners background color in xml like this:

android:background="YOUR_HEX_COLOR_CODE"

and if you use the drop down menu with you spinner you can set its background color like this:

android:popupBackground="YOUR_HEX_COLOR_CODE"

To change text color I recommended to use custom text layout

Give your customized color and size to text in this file.

spinner_item.xml

<?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="YOUR_HEX_COLOR_CODE"         
    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.

Upvotes: 3

Nitin Patel
Nitin Patel

Reputation: 1651

Try this..Change Style in In res/values/styles.xml::-

<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:spinnerDropDownItemStyle">@style/mySpinnerItemStyle</item>
</style>

<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
    <item name="android:textColor">@android:color/white</item>
</style>

Upvotes: 4

Related Questions