Change spinner items background color in android programmatically

How to add background color to all spinner items so they look different.

Background color makes the spinner items look different then the whole layout background because they look more bright and colorful. This type of functionality can be possible when we have add extra textview_with_background.xml file into our project and set the textview into spinner. So textview text become the spinner items background text. So here is the complete step by step tutorial for Change spinner items background color in android programmatically.

android-project-download-code-button

How to Change spinner items background color in android programmatically.

Code for MainActivity.java file.

 package com.spinneritembackgroundcolor_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
 Spinner spinnerBackgroundChange;
 
 String[] spinnerItems = new String[]{
 "APPLE",
 "MANGO",
 "BANANA",
 "PAPAYA",
 "WATER MELON",
 
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
 
 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
 this,R.layout.textview_with_background,spinnerItems );
 
 spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
 
 spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);
 
 spinnerBackgroundChange.setOnItemSelectedListener(new OnItemSelectedListener() {

 @Override
 public void onItemSelected(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 Toast.makeText(MainActivity.this, spinnerBackgroundChange.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
 
 }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {
 // TODO Auto-generated method stub
 
 }
 });
 
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.spinneritembackgroundcolor_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="250dp"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

Code for textview_with_background.xml file.

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/textView1"
 android:layout_width="250dp"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textColor="#fefcfe"
 android:padding="10dp"
 android:textSize="18dp"
 android:background="#cf01fb" />

Screenshot:

Change spinner items background color in android programmatically

Click here to download Change spinner items background color in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *