Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide text of Android spinner

Tags:

android

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.

like image 720
indira Avatar asked Dec 04 '22 09:12

indira


2 Answers

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) {}  
 });
like image 76
tvoloshyn Avatar answered Jan 09 '23 11:01

tvoloshyn


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.

like image 44
Melinda Green Avatar answered Jan 09 '23 10:01

Melinda Green