views:

202

answers:

3

In my searchable.xml in my android project, I get to specify a hint that appears in the edittext, when no text is written. Is there any way to dynamically set this?

In my action, I have two search buttons, that are calling startSearch passing their own parameters. I would like to set hints that reflect the selected action, based on which button was clicked - say "search movie titles", "search actors".

The way I see it, this could potentially be achievable by passing parameters to startSearch, or using a localization approach, just as I could place one hint in values-fr\strings.xml, there might be an alternative search resource file to target for when another button has been clicked? Or if the searchable.xml could be made into a selector, so that I could have it act differently in different states, somehow, that would also be fine... Problem is I haven't been able to find a means of achieving any of these.

The real reason I want to be doing this, is because the way I see it, it's the best way of communicating that the default action, when the device search button is pressed, is the first option, title search.

UPDATE

To avoid confusion, I'm happy with any declarative or programmatic approach of changing the hint in the EditText mSearchTextField in SearchDialog. What I'm unclear about is how to reference that EditText. The comments in the code linked to says "This is still controlled by the SearchManager ..." but I can't find any reference to how it can be controlled from the SearchManager either.

+1  A: 

Android's XML doesn't lend itself too well to dynamism; you'll probably have to fix this problem in the Activity's code.

You could do this in code by changing the hint in the EditText by using setHint(). Then you'd just need a listener of some kind on the buttons (or possibly a Spinner). For a Button, you'd just use setOnClickListener(); for a spinner setOnItemSelectedListener() would be more appropriate.

Daniel Lew
Setting the hint programmatically would be quite fine by me, but how do I reference the EditText that is shown in `onSearchRequested` (or `startSearch`) ?
David Hedlund
(also, see the update to my question)
David Hedlund
+1  A: 

AFAIK, There isn't any way you can access the EditText provided for search.

Karan
A: 

I'm not sure what your startSearch looks like, but something like this is what you are looking for:

private void startSearch(CharSequence hint) {
    EditText searchBox = (EditText)findViewById(R.id.my_edit_text);
    searchBox.setHint(hint);
Segfault
`startSearch` is the native function available in any Activity, and pretty much specifically what I'm asking for is if there's any way to access the `EditText` (i.e. your `findViewById` but the `EditText` is created by Android, and not by my application specifically - through `startSearch` - and as I haven't created it, I don't have an ID for it)
David Hedlund
OIC, so what element are you adding the hint attribute to in your searchable.xml?
Segfault