jowan
jowan

Reputation: 67

How to change title in search box?

I tried to add android:title="" that doesn't work.

SearchView searchView = (SearchView) miFind.getActionView(); searchView.setQueryHint("your message");

didn't work as well. enter image description here

private SearchView.OnQueryTextListener onSearch() {
        return new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String query) {

                WebView mWebView= (WebView) findViewById(R.id.webView);
                mWebView.loadUrl("http://www.example.com" +query);
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        };

enter image description here

Upvotes: 1

Views: 668

Answers (2)

forpas
forpas

Reputation: 164089

If you have defined in your menu an item with this attribute:

app:actionViewClass="android.support.v7.widget.SearchView"

then you must get it in code in order to change its hint.

In onCreateOptionsMenu() method, add this code after you inflate the menu:

MenuItem miFind = menu.findItem(R.id.miFind);
SearchView searchView = (SearchView) miFind.getActionView();
searchView.setQueryHint("your message");

replace miFind with the id of the menu item which acts as your SearchView.

Edit: In onCreateOptionsMenu() add this listener:

miFind.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
     @Override
      public boolean onMenuItemActionExpand(MenuItem menuItem) {
          searchView.setQueryHint("your message when expanded");
          return true;
      }

      @Override
      public boolean onMenuItemActionCollapse(MenuItem menuItem) {
           searchView.setQueryHint("your message when collapseded");
           return true;
      }
});

Upvotes: 1

André Sousa
André Sousa

Reputation: 1730

If you are talking about the search hint, you need use this method from SearchView: setQueryHint

From the documentation:

Sets the hint text to display in the query text field. This overrides any hint specified in the SearchableInfo.

This value may be specified as an empty string to prevent any query hint from being displayed.


Example:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

    <android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:imeOptions="actionSearch|flagNoExtractUi"
            app:searchHintIcon="@null"
            app:searchIcon="@null"
            app:queryBackground="@null"
            app:submitBackground="@null"/>

</android.support.v7.widget.Toolbar>

Java:

final SearchView searchView = findViewById(R.id.search_view);
searchView.setQueryHint("Your search string");

Upvotes: 1

Related Questions