0

I have a spinner with a list of article "sections" - "news, sports, local, obits..." etc.

It also doubles as the header for the section, so the user knows what articles they're looking at.

When the user does a search, it auto-selects the "Search" section, so the spinner is on "Search", and it's displaying the search results. The problem is, "Search" is not a section, so I don't want the user to be able to manually select "Search" section, as nothing would happen.

I thought I could just remove "Search" from the XML file that generates that spinner options, but then how can I manually set the text of the spinner to "Search" when they do a search?

Is there a better/another way to do this that I'm not thinking of?

Dave
  • 28,833
  • 23
  • 113
  • 183

1 Answers1

0

This was taken from this answer that I posted previously - setPrompt on Spinner doesn't work

Pretty much in answer to your question - no, there are a few ways around this however - this post will demonstrate one, and a few others will be in the link at the bottom

You could use a button and create a drop down list from that.

First, in your layout replace your spinner with a button, and give it the text that you gave your prompt

Then move your list of items on yoour spinner to a file called optionmenu.xml

Then declare your button and set up an onClickListener for it.

Inside that method, put the following code

On the foutrh line, the layout that you specify is your optionmenu layout

public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.layout.optionmenu, popup.getMenu());
popup.show();
} When you call this method, you need to do the following

    View p = (View)findViewById(R.id.spinner button);
showPopup(p);

In this price of code, View p is the view at the location that you want to show the top corner of the menu. So in this case, it will be your button

See the answer here to implement it - android menu code not working

And in each case: statement, put a line that sets your button text as a string.

case R.id.item1:
button.setText("item1");

There are some other good solutions here -

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54