1

I want to set the hint in spinner. Like in edit text, I am giving the hint, that is:

android:Hint="First Name"

So what should I do for Spinner like for state?

My state will dynamically fill from the database. If I give the hint in array index[0], then it will effect on zeroth position of that field.

How can I do it?

Code

public String[] getState()
{
    try
    {
       Cursor cursor = dbUser.State();
       if (cursor.getCount() >= 0)
       {
           array_state = new String[cursor.getCount()];
           i = 0;
           while (cursor.moveToNext())
           {
              array_state[i] = cursor.getString(1);
              i++;
           }
        }
     }
     catch (Exception e)
     { 
         // TODO: handle exception
     }
     return array_state;
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1061793
  • 1,047
  • 8
  • 19
  • 27
  • 3
    No...How Toast message..I want to show it into the spinner that hint is what should you fill here..like if state is there..then.like this type – user1061793 Dec 19 '11 at 06:17

2 Answers2

4

Use android:prompt="select your fruit" attribute for the <Spinner>.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • ArrayAdapter ad_state = new ArrayAdapter( this,android.R.layout.simple_spinner_item,array_state); ad_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); state.setAdapter(ad_state); – user1061793 Dec 19 '11 at 06:35
  • private String array_state[]; this is the way to initlize – user1061793 Dec 19 '11 at 06:36
  • public String[] getState() { try { Cursor cursor = dbUser.State(); if (cursor.getCount() >= 0) { array_state = new String[cursor.getCount()]; i = 0; while (cursor.moveToNext()) { array_state[i] = cursor.getString(1); i++; } } } catch (Exception e) { // TODO: handle exception } return array_state; } – user1061793 Dec 19 '11 at 06:36
  • and this is the method to fill spinner from the database at runtime.. – user1061793 Dec 19 '11 at 06:37
  • What is this? My dear update your question with this code actually. – Paresh Mayani Dec 19 '11 at 06:37
  • 15
    That will display on the Dialog as Title where the Spinner items are being shown not as hint – ingsaurabh Dec 19 '11 at 06:38
  • @PareshMayani please see the above comment. It cannot be shown as a hint... – Avadhani Y May 23 '13 at 11:10
  • The android:prompt does not take string values. – Nirab Pudasaini Jul 25 '14 at 04:47
1

What you can do is when you implement onSelectionListener of Spinner just neglect the zeroth position and display your hint at zeroth position

Update

change your code to this

array_state = new String[cursor.getCount() + 1];
array_state[0] = "Hint String";
i = 1; 
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81