anuj
anuj

Reputation: 429

Spinner prompt not showing

enter image description hereI have a Profession's spinner(drop-down) in which i have list of professions.I want to show the default value as "Select Profession".In my xml i type android:prompt="Select Profession" but nothing is showing up.I wanted "Select Profession" to be shown at the spot where i have marked its as red

Spinner.XML

  <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/sp_profession"
                    android:layout_weight="1"
                    style="@style/spinner"
                    android:prompt="Select Profession"
                    android:spinnerMode="dropdown"
                    android:layout_margin="2dp"></Spinner> 

I did doing something like this but i am getting null value at prompt_text

profession_array = getResources ().getStringArray (R.array.Profession);
        profession_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, profession_array);
        prompt_text.setText ("Select Profession");
        profession_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);

R.layout.textview_spinner

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/prompt_text"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:minHeight="1dp"
    android:gravity="center"
    android:textSize="20sp"
    android:textColor="@android:color/white" />

Upvotes: 14

Views: 41690

Answers (4)

John Schmitt
John Schmitt

Reputation: 11

I had the same problem and style="@android:style/Widget.Spinner" was the solution for me as well. just insert into the Spinner tag without the android: preface

Upvotes: 1

Gordon
Gordon

Reputation: 65

you should set style ---> style="@android:style/Widget.Spinner" works for me. Hope it help.

Upvotes: 6

Hareesh S
Hareesh S

Reputation: 69

Not the correct way but it works.. What ever you wan to show give it as the first item in the String array like this

string.xml

 <string-array name="Profession">
    <item>Please select the Profession</item>
    <item>Student</item>
    <item>Prof</item>
    <item>staff</item>
    <item>research student</item>

in java code when ur reading from spinng object

Spinner profession = (Spinner)findViewById(R.id.profession);

String prof = String.valueOf(profession.getSelectedItem());


if(prof.equals("Please select the Profession"))
    {

      Toast.makeText(getApplicationContext(), "Please select the Profession", Toast.LENGTH_SHORT).show();

    }else{
   //Do your thing here....
    }

Upvotes: 6

Araju
Araju

Reputation: 549

Prompt is used to show title on dropdown popup not for default text.

I think you are looking for setting the default value on spinner when you have not selected any value from spinner dropdown. For that you need to use NothingSelectedSpinnerAdapter, below is the link for more details :

https://stackoverflow.com/a/12221309/2389804

Upvotes: 11

Related Questions