Search code examples
androidandroid-actionbarsearchviewandroid-actionbar-compat

search view with back icon in android


In my project I am using search view of android. It is working properly.

But I am having one problem i.e related to back image while searchview expands.

enter image description here

This is my action bar with icon and other search and drawable icon.

Now when I click on search Icon

enter image description here

Here Icon is changes. It's by default taking launcher image. but I want it to be same as its showing in image 1.

So can any one help me with this issue.

edit1

here is my action bar style xml.

 <style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarItemBackground">@drawable/selectable_background_example</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.Example</item>
    <item name="actionModeBackground">@drawable/cab_background_top_example</item>
    <item name="actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
    <item name="actionBarTabTextStyle">@style/ActionBarTabText.Example</item>
    <!-- Light.DarkActionBar specific -->
    <item name="android:textColorHighlight">#99c0c0c0</item>
</style>

edit2

substyle for actionbar

 <style name="ActionBar.Solid.Example"   parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:icon">@drawable/ic_logo</item>
    <item name="background">@drawable/ab_solid_example</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_example</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_example</item>
</style>

edit 3

This is my onCreateOptionsMenu method.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    try {
        search = (MenuItem) menu.findItem(R.id.search);

        MenuItemCompat.setOnActionExpandListener(search,
                new OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem item) {
                        // Do something when collapsed

                        layoutList.setVisibility(View.INVISIBLE);
                        return true; // Return true to collapse action view
                    }

                    @Override
                    public boolean onMenuItemActionExpand(MenuItem item) {
                        // Do something when expanded

                        layoutList.setVisibility(View.VISIBLE);
                        return true; // Return true to expand action view
                    }
                });

        SearchView searchView = (SearchView) MenuItemCompat
                .getActionView(search);

        setSearchTextColour(searchView);

        searchView.setQueryHint(getString(R.string.search_hint));
        searchView.setOnQueryTextListener(this);
        searchView.setOnCloseListener(new OnCloseListener() {

            @Override
            public boolean onClose() {
                // TODO Auto-generated method stub
                Debugger.debugE("on close");

                layoutList.setVisibility(View.INVISIBLE);
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

    return super.onCreateOptionsMenu(menu);
}

and to show actionbar

    ActionBar actionBar = getSupportActionBar();
    SpannableString s = new SpannableString(getString(R.string.app_name));
    s.setSpan(new TypeFaceSpan(getContext(),
            getString(R.string.font_gothum)), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.setTitle(s);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#bb0404")));

There is nothing else that I had used.

note: In all other navigation from the main screen icon which is showing in first image is coming. Issue is only with searchview.

Thanks in advance.

Bskania


Solution

  • Try changing your app theme on your styles.xml

    You can do something like this:

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/ActionBar</item>
    </style>
    
    <style name="ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:icon">@drawable/myicon</item>
    </style>