Thursday, October 13, 2016

android - Set Spinner prompt text

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="#c78ef9"
    android:padding="25dp"
    >
    <!--
        android:prompt
            The prompt to display when the spinner's dialog is shown.

            Must be a reference to another resource, in the form "@[+][package:]type:name" or
            to a theme attribute in the form "?[package:][type:]name".

            This corresponds to the global attribute resource symbol prompt.
    -->
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog"
        android:prompt="@string/prompt_text"
        />
</android.support.design.widget.CoordinatorLayout>
res/values/strings.xml

<resources>

    <string name="app_name">Android Example</string>
    <string name="title_activity_main">Example</string>

    <string name="prompt_text">Choose one</string>
</resources>

2. Set Spinner prompt text programmatically

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.widget.ArrayAdapter;
import android.widget.Spinner;

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;


    @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 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
        );

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

        /*
            setPrompt

                void setPrompt (CharSequence prompt)
                Sets the prompt to display when the dialog is shown.

                Parameters
                    prompt CharSequence: the prompt to set
        */

        // Set spinner prompt text
        // Also ensure that spinner mode is dialog in layout xml
        mSpinner.setPrompt("Select one");
    }
}