4

I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the selected item.

indira
  • 6,569
  • 18
  • 65
  • 80
  • duplicate question but with no answer: http://stackoverflow.com/questions/4342273/how-do-i-hide-the-text-on-a-spinner – Harry Joy Feb 08 '11 at 09:07
  • I got across the same problem but i found my 9 patch png invalid and hence,fixing 9 patch and cleaning-building project worked for me. Can you please post your code so that it is easy for us to suggest you something. – Hiral Vadodaria Jan 19 '12 at 04:36
  • http://stackoverflow.com/questions/15479579/remove-text-from-spinner - your solution – Yuriy Vasylenko Mar 29 '15 at 09:11

3 Answers3

15
Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);  
spin.setOnItemSelectedListener(new OnItemSelectedListener() {  
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
// hide selection text  
((TextView)view).setText(null);  
// if you want you can change background here  
}  
  public void onNothingSelected(AdapterView<?> arg0) {}  
 });
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
tvoloshyn
  • 407
  • 8
  • 22
2

Create ghost_text.xml in your layouts folder with this text:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textColor="@android:color/transparent" />

Then use that file in your adapter like this:

    ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this, 
        R.layout.ghost_text, your_list);
    your_spinner.setAdapter(your_adapter);

You should also be able to use this technique with other resource-based adapters.

Melinda Green
  • 2,100
  • 1
  • 21
  • 32
0

Probably you can define a xml-layout file, without any textview in it, and supply it to the adapter for the spinner.

e.g. empty_spinner_item.xml

<ImageView xmlns:android="http..."
    android:layout_width=".." 
/>

and then use any adapter:

spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));
xandy
  • 27,357
  • 8
  • 59
  • 64
  • I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the selected item. – indira Feb 08 '11 at 14:56
  • 1
    I can't hide the text of spinner. So I replace the spinner with a button. When button clicks, the spinner pop ups (spinner.performClick). I also wrote a OnItemSelectedListener to update the TextView with the selected item. But OnItemSelectedListener is not working :(. Any help please.. – indira Feb 09 '11 at 04:00
  • 1
    It works. StorageSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { tv.setText(StorageSpinner.getSelectedItem()+""); } public void onNothingSelected(AdapterView> arg0) {} }); – indira Feb 09 '11 at 04:31
  • When I tried this, my app crashed with the error "ArrayAdapter requires the resource ID to be a TextView." – arlomedia Aug 19 '14 at 00:34