14

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" />
anuj
  • 429
  • 3
  • 7
  • 14
  • add your first element as "Select Profession" – Ameer May 22 '14 at 12:25
  • honestly, i don't understand why google doesn't use their setPrompt API for exactly this purpose. especially when the prompt doesn't show up in the dropdown since API 14 – deviant Oct 13 '21 at 13:46

4 Answers4

11

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

Community
  • 1
  • 1
Araju
  • 539
  • 8
  • 20
6

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....
    }
Hareesh S
  • 69
  • 1
  • 2
  • 1
    If you are planning to put multiple languages in your application, than it's not a good approach. I think that case you should use getSelectedItemPosition() method (== 0) or something like that if you want to use this solution. – wyzard Aug 31 '18 at 12:03
6

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

Gordon
  • 65
  • 1
  • 3
1

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

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109