indira
indira

Reputation: 6687

Hide text of Android spinner

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.

Upvotes: 4

Views: 11191

Answers (3)

tvoloshyn
tvoloshyn

Reputation: 407

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) {}  
 });

Upvotes: 15

Melinda Green
Melinda Green

Reputation: 2130

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.

Upvotes: 2

xandy
xandy

Reputation: 27411

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, ... ));

Upvotes: 0

Related Questions