Thursday, October 13, 2016

android - Customize Spinner style

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f1f0e4"
    android:padding="25dp"
    >
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Spinner mSpinner;
    private ArrayAdapter mAdapter;
    private Typeface mTypeface;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mSpinner = (Spinner) findViewById(R.id.spinner);

        // Initialize the custom typeface
        mTypeface = Typeface.createFromAsset(getAssets(),"fonts/CourgetteRegular.ttf");

        // Initialize a new list with values
        List<String> flowers = new ArrayList<>();
        flowers.add("Aquilegia");
        flowers.add("Astilbe");
        flowers.add("Astrantia");
        flowers.add("Aubreita deltoidea");
        flowers.add("Bee Balm Flower");

        // Initialize an array adapter
        mAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,flowers){
            public View getView(int position, View convertView, ViewGroup parent) {
                // Cast the spinner collapsed item (non-popup item) as a text view
                TextView tv = (TextView) super.getView(position, convertView, parent);

                // Set the text color of spinner item
                tv.setTextColor(Color.parseColor("#FF9B0CC2"));

                // Set the typeface/font of spinner item
                tv.setTypeface(mTypeface);

                // Set spinner item text/font size
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);

                // Return the view
                return tv;
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent){
                // Cast the drop down items (popup items) as text view
                TextView tv = (TextView) super.getDropDownView(position,convertView,parent);

                // Set the items typeface
                tv.setTypeface(mTypeface);

                // Set spinner items text/font size
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);

                if(position%2 == 1){
                    // Set those items text color
                    tv.setTextColor(Color.RED);
                    // Set the spinner items background color
                    tv.setBackgroundColor(Color.parseColor("#beffbd"));

                }else {
                    // Set alternate items text color
                    tv.setTextColor(Color.BLUE);
                    // Set the spinner alternate items background color
                    tv.setBackgroundColor(Color.parseColor("#FF8AAB89"));
                }

                // Return the modified view
                return tv;
            }
        };

        // Data bind the spinner with array adapter items
        mSpinner.setAdapter(mAdapter);
    }
}