Friday, August 14, 2015

How to set Spinner alternate item color in Android

MainActivity.java

package com.cfsuman.androidtutorials;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the widgets reference from XML layout
                Spinner spinner = findViewById(R.id.spinner);


                // Initializing a String Array
                String[] plants = new String[]{
                        "Laceflower",
                        "California sycamore",
                        "Mountain mahogany",
                        "Butterfly weed",
                        "Carrot weed"
                };


                // Convert array to a list
                List<String> plantsList = new ArrayList<>
                        (Arrays.asList(plants));


                // Initializing an ArrayAdapter
                ArrayAdapter<String> spinnerArrayAdapter
                        = new ArrayAdapter<String>(
                        this,
                        android.R.layout.simple_dropdown_item_1line,
                        plantsList
                ){
                        public View getDropDownView(
                                int position, View convertView,
                                @NonNull ViewGroup parent) {

                                View view = super.getDropDownView(
                                        position, convertView, parent);
                                TextView textView = (TextView) view;
                                if(position%2 == 1) {
                                        // Set the item text color
                                        textView.setTextColor(
                                                Color.parseColor("#0018A8"));
                                        // Set the item background color
                                        textView.setBackgroundColor(
                                                Color.parseColor("#FEFEFA"));
                                }
                                else {
                                        // Alternate item text color
                                        textView.setTextColor(
                                                Color.parseColor("#A2006D"));
                                        // Alternate item background color
                                        textView.setBackgroundColor(
                                                Color.parseColor("#FFFAF0"));
                                }
                                return view;
                        }
                };


                // Set the drop down view resource
                spinnerArrayAdapter.setDropDownViewResource(
                        android.R.layout.simple_dropdown_item_1line
                );

                // Finally, data bind the spinner object with adapter
                spinner.setAdapter(spinnerArrayAdapter);
        }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="64dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>