Wednesday, October 12, 2016

android - Spinner on item selected listener

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="#aab7a7"
    android:padding="25dp"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select an item."
        android:gravity="center"
        android:layout_gravity="center_horizontal|top"
        android:background="#c364e4"
        android:layout_margin="10dp"
        android:padding="15dp"
        android:textStyle="bold"
        />
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/text_view"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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


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

    private CoordinatorLayout mCLayout;
    private TextView mTextView;
    private Spinner mSpinner;
    private ArrayAdapter mAdapter;


    @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);
        mTextView = (TextView) findViewById(R.id.text_view);
        mSpinner = (Spinner) findViewById(R.id.spinner);

        // Initialize a new list
        List<String> flowers = new ArrayList<>();
        flowers.add("Aconitum");
        flowers.add("African Daisy");
        flowers.add("Alchemilla");
        flowers.add("Allium roseum");
        flowers.add("Alstroemeria");

        // Initialize an array adapter
        mAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,flowers);
        mAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

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

        /*
            AdapterView.OnItemSelectedListener
                public static interface AdapterView.OnItemSelectedListener
                android.widget.AdapterView.OnItemSelectedListener

                Interface definition for a callback to be invoked when an item in this view has been selected.
        */
        // Set an item selection listener for spinner widget
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            /*
                onItemSelected
                    void onItemSelected (AdapterView<?> parent,
                                    View view,
                                    int position,
                                    long id)

                    Callback method to be invoked when an item in this view has been selected.
                    This callback is invoked only when the newly selected position is different
                    from the previously selected position or if there was no selected item.

                    Impelmenters can call getItemAtPosition(position) if they need to access the
                    data associated with the selected item.

                    Parameters
                        parent AdapterView: The AdapterView where the selection happened
                        view View: The view within the AdapterView that was clicked
                        position int: The position of the view in the adapter
                        id long: The row id of the item that is selected
            */
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // Get the spinner selected item text
                String selectedItemText = (String) adapterView.getItemAtPosition(i);
                // Display the selected item into the TextView
                mTextView.setText("Selected : " + selectedItemText);
            }

            /*
                onNothingSelected

                    void onNothingSelected (AdapterView<?> parent)
                    Callback method to be invoked when the selection disappears from this view.
                    The selection can disappear for instance when touch is activated or when
                    the adapter becomes empty.

                    Parameters
                        parent AdapterView: The AdapterView that now contains no selected item.
            */
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Toast.makeText(mContext,"No selection",Toast.LENGTH_LONG).show();
            }
        });
    }
}