Android spinner background color

Change spinner background color but keep arrow

I’ve read several thing about it but I can’t find what I need. I want to keep the grey arrow but I want to remove the horizontal bar from the default style and have a white background. Do you have any idea of how I can do this ?

Here is what I have now (default spinner style) :

Here is what I want :

11 Answers 11

I did a little modification based on @Mansur Khan ‘s answer.

We don’t have to add an ImageView in this case because the spinner already has a triangle arrow. So check the code below:

Here is the screenshot

For the record, I found an easy solution : Wrap your spinner in a relative layout and add an image :

A simple solution that doesn’t require you to create your own drawable for the arrow is to wrap the spinner with a RelativeLayout , and set the background color in the RelativeLayout , not the spinner:

and your class should implement OnItemSelectedListener.

Hi instead of wrapping Spinner component around Parent Layouts like LinearLayout, RelativeLayout etc which increases layout parsing simply create a drawable called spinner_bg.xml under drawable folder.

Set spinner_bg as the background of your spinner and it works like charm:

I think the best way without doing complex layouts is this:

Use this xml for your spinner background and you are good to go.

A good way to customise spinners and any other Android controls is to use the Android Asset Studio site and choose the Android Holo Colors Generator. This will create all the assets you might need, including the «underline». It also generates the XML files that implement the changes.

Читайте также:  Android the markup in the document preceding the root element must be well formed

Once you download the ZIP file from that site, you just copy the images and XML files into your project.

You can then edit the required image files to remove the underline. They are 9-Patch files, called:

  • apptheme_spinner_default_holo_light.9.png
  • apptheme_spinner_disabled_holo_light.9.png
  • apptheme_spinner_focused_holo_light.9.png
  • apptheme_spinner_pressed_holo_light.9.png

For a more complete explanation of the process, and to see some of the sample XML files, please refer to my related answer:

Источник

How to change the spinner background in Android?

I am developing an app in which I need to change the spinner background layout to match the background color. I researched and found that I need to create a 9 patch image. I have done creating the 9 patch image and used in the app but it looks bigger than the normal spinner and also I couldn’t see the drop down button in the spinner as well.

I am so happy if you guys provide me a clear tutorial from start creating the 9 patch image for Spinner and using it in the app.

Code for the Spinner

15 Answers 15

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

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

You can change background color and drop down icon like doing this way

Step1: In drawable folder make background.xml for border of spinner.

Step2: for layout design of spinner use this drop down icon or any image drop.pnj

Finally looks like below image and it is every where clickable in round area and no need of to write click Lister for imageView.

For more details , you can see Here

Even though it is an older post but as I came across it while looking for same problem so I thought I will add my two cents as well. Here is my version of Spinner’s background with DropDown arrow. Just the complete background, not only the arrow.

This is how it looks..

Apply on spinner like.

Читайте также:  Guitar полная версия андроид

@color/InputBg should be replaced by the color you want as your background.

First it fills the background with desired color. Then a child layer-list makes a square and rotates it by 45 degrees and then a second Rectangle with background color covers the top part of rotated square making it look like a down arrow. (There is an extra stroke in rotated rectangle with is not really required)

Источник

How to Change the background color of Spinner selection in Android

In the above picture GAIN 3 is selected but its not visible properly , so how can i change that color to darker color. basically i want to change the selected text background in darker color.

I’m using com.jaredrummler.materialspinner.MaterialSpinner Spinner.

Here’s the java implementation.

And the layout item looks like the following.

4 Answers 4

To change background color and other color this library has provided some attributes. To change background color of selected item use below code.

create one selector in drawable having name selector_gray_white_spinner.xml

Add dark color in your color.xml file

Use this way it will help you:

There are some attributes available along with the implementation of that specific library. Please have a look in the readme.md section where the attributes are listed.

I think you might consider using ms_background_selector attribute in your layout where you have declared the spinner.

So the layout declaration will look like this.

Add a file named your_darker_selector.xml and put the following code inside the file.

Modify the color from the selector file as per your necessity.

Источник

How to customize a Spinner in Android

I want to add a custom height to the dropdown of a Spinner , say 30dp, and I want to hide the dividers of the dropdown list of Spinner .

So far I tried to implement following style to the Spinner :

and the code of my spinner is :

But nothing seems to be working.

6 Answers 6

Create a custom adapter with a custom layout for your spinner.

In styles add your custom dimensions and height as per your requirement.

Читайте также:  Что делать если не помнишь пароль от планшета андроид

You can create fully custom spinner design like as

Step1: In drawable folder make background.xml for a border of the spinner.

Step2: for layout design of spinner use this drop-down icon or any image drop.png

Finally looks like below image and it is everywhere clickable in round area and no need to write click Lister for imageView.

Step3: For drop-down design, remove the line from Dropdown ListView and change the background color, Create custom adapter like as

In layout folder create R.layout.spinner_text.xml

In layout folder create simple_spinner_dropdown.xml

In styles, you can add custom dimensions and height as per your requirement.

Источник

Изменить цвет фона выбранного элемента в Android Spinner

Я работаю над приложением для Android и использую Spinner во многих местах своего приложения. Я хочу изменить цвет фона выбранного элемента прядильщика, чтобы можно было легко определить, какой элемент выбран в данный момент.

Я хочу изменить цвет выбранного элемента в списке, а не на счетчике, см. Изображение ниже.

Как я могу это сделать? Пожалуйста, кто-нибудь может мне помочь?

Большое спасибо заранее.

6 ответов

Вам необходимо реализовать метод ниже в своем классе адаптера:

Это поможет вам:

Теперь об элементе, выбранном в счетчике, поместите ниже

Вот решение через XML:

Спиннер выглядит так:

При создании счетчика установите setDropDownViewResource как пользовательский макет:

И spinner_item.xml выглядит так:

И, наконец, мы устанавливаем @ drawable / spinner следующим образом:

Надеюсь, мой ответ будет вам полезен!

Создайте int переменную public static int posOfItemSpinnerSelected в своей деятельности:

И в свой адаптер вставьте этот код

Определите цвет фона Spinner в папке цветов ..

Попробуйте создать селектор в drawable, например,

И установите фон спиннера как

Я искал в Интернете подходящее решение, чтобы сделать это без жесткого кодирования фонового поведения в java-коде. Вы можете добиться этого (установив цвет фона выбранного элемента) с помощью чертежей.

Что вам нужно сделать, это установить dropdownViewResource на собственный макет. Этот макет должен выглядеть примерно так:

Источник

Оцените статью