0

I know there are lot many solutions for the same kind of question on the web, but still I couldn't find one to serve my needs.

I have a spinner in my layout xml and a string array in strings.xml .

<string-array name="days">
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
    </string-array>

and the java code as

Spinner days=(Spinner) findViewById(R.id.days_1);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this,
R.array.days,R.layout.support_simple_spinner_dropdown_item);
    days.setAdapter(adapter);

I want to set up hint for spinner.

I have already read other answers for the same question but when I add an item in the string-array

<string-array name="days">
            <item>Monday</item>
            <item>Tuesday</item>
            <item>Wednesday</item>
            <item>Thursday</item>
            <item>Friday</item>
            <item>Saturday</item>
            <item>Select the Day</item>  <!--New item-->
        </string-array>

and in java after this line

days.setSelection(/*last element*/);

what happens is that the hint becomes permanent in the drop-down list. It is not working as the hint in edittext.

Please suggest me some method to set the hint for that.

Thanks in advance.

ks_mani
  • 169
  • 1
  • 5
  • 18
  • Its there in the dropdown because you are adding it in the adapter. To set a hint, you'll have to put a textView in the adapter statically. BUT no matter what you select, that text will be displayed (Not in the list though But as the selection). – Tushar Gogna Feb 13 '15 at 11:11
  • Please go through this link it might be helpful for what you want to do. [How to make an Android Spinner with initial text “Select One”](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – BBdev Feb 13 '15 at 11:13

1 Answers1

0

You can try this Change your string array to

    <string-array name="days">
        <item />
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>            
    </string-array>

Now write on item selected listener for your spinner

    days.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            TextView tv = (TextView) days.getChildAt(position);
            if (position == 0) {

                if (tv != null) {
                    tv.setText("Select the Day");
                    tv.setTextColor(Color.GRAY);
                }
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

That's it..Enjoy...