Prateek
Prateek

Reputation: 316

Change the icon of default Search View android

I am facing the problem with default search view icon.I want to change the icon in my application and use custom drawable instead of default.

I am using the xml:

 <SearchView
                    android:id="@+id/searchView1"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp" >
                </SearchView>

Thanks for help.

Upvotes: 0

Views: 1228

Answers (1)

Irshad Khan
Irshad Khan

Reputation: 784

here i am giving the code for searchView used in whatsApp like

your activity_main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <TextView
           android:id="@+id/status_text"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal"/>
</LinearLayout>

your searchview_in_menu.xml in menu folder

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_search"

         android:title="Search"
         android:icon="@android:drawable/ic_menu_search"
         android:showAsAction="always"
         android:actionLayout="@layout/searchview_layout"/>
</menu>

your searchview_layout.xml in layout folder

<?xml version="1.0" encoding="utf-8"?>
    <SearchView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/search_view_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

your MainActivity.java is

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SearchView;

public class MainActivity extends Activity {
    private SearchView mSearchView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        //find the search view item and inflate it in the menu layout
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        //set a hint on the search view (optional)
        mSearchView.setQueryHint(getString(R.string.search));
        //these flags together with the search view layout expand the search view in the landscape mode
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                | MenuItem.SHOW_AS_ACTION_ALWAYS);
        //expand the search view when entering the activity(optional)
        searchItem.expandActionView();
        return true;
    }

it will produce result like the following screenshot enter image description here

enter image description here

Hope it will serve your purpose.

Upvotes: 1

Related Questions