Custom Spinner in Android To Change Background and Text Colors

When writing a recent app, I wanted to use a spinner. No problem! Worked great in the emulator – grey background with black text. I checked it out on a few physical devices and I was getting white text on a grey background. Absolutely useless. I found way so changing the text inside of the selected box, but was still getting white text on a grey background when I was presented with the options to select from. Everything I had found essentially said to me “write your own!” but wasn’t given any idea of HOW to do that. Well, I’m going to share how I wrote my own.

I already like the layout that the simple_dropdown_item_1line option provided. My code looked like so:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, list);

In Eclipse, hold control and click on the simple_dropdown_item_1line item. This brought up the code that was originally used. Aha! Now, just need to copy this and make my own and reference it! I copied the body of the simple_dropdown_item_1line.xml file to my res/layout/ directory and modified it to add in the following:

android:textColor=”#000000″
android:background=”#ddd”

the #ddd is the grey color that is used in the spinner by normal, and the #000000 is to specify black color text. With that modification in place, time to change my reference from the built-in android to my custom spinner now. I changed the array adapter to the following:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.dropdown_item_1line, list);

In case you didn’t see the difference, it is not using android.R.layout… but the local R.layout… instead. The color changes in the class change the selected text and background as well as the dialog text and background.

With that 4 minute change, I had fixed the thing that was bothering me! Hope this helps someone else out there!

Edit: Whoops! Never realized that I had the wrong colors mentioned when using the emulator. Fixed!

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a comment