Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
like image 745
anuj Avatar asked May 22 '14 11:05

anuj


4 Answers

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

like image 78
Araju Avatar answered Nov 14 '22 00:11

Araju


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

like image 24
John Schmitt Avatar answered Nov 14 '22 01:11

John Schmitt


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....
    }
like image 6
Hareesh S Avatar answered Nov 13 '22 23:11

Hareesh S


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

like image 5
Gordon Avatar answered Nov 14 '22 00:11

Gordon