Search View uses app icon instead of logo

Android documents explain that app logo is used everywhere when it is defined. But when the search view expands, app icon is used instead of app logo and I can't find a way to show app logo when search view is in expanded state.

Here are the relevant parts.

Manifest file

<application
    android:name="MyApp"
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:logo="@drawable/app_logo"
    android:theme="@style/Theme.MyTheme" >

searchable.xml (setting icon doesn't change anything)

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:icon="@drawable/app_logo"
    android:label="@string/app_name"
    android:hint="@string/search_hint" >
</searchable>

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/ic_search"
        android:showAsAction="always|collapseActionView" />

Activity Code

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.menu_search).setActionView(searchView);

NOTE: I use ABS SearchView but same thing happens when I switch to default SearchView.

EDIT : I added some screenshots for clarity.

Here I used the star image for app logo, and android image for app icon. The first screenshot is the default view of activity. The second one is the view when I click search button. It shows the android image while I expect it to be the star image.

Answers


Set the logo drawable as your icon. Via theme attribute

<item name="android:icon">@drawable/ab_logo</item>

or in code

getSupportActionBar().setIcon(R.drawable.ab_logo); 

You're using a completely different SearchView you declared in menu.xml.

With this code you create new SearchView, which is not what you want.

SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());

So try creating menu like this.

this.getSupportMenuInflater().inflate(R.menu.menu, menu);
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
final MenuItem searchItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(info);

And for the ActionBar, instead of setDisplayUseLogoEnabled(true) and others, try set all options at once

final ActionBar bar = getSupportActionBar();
bar.setDisplayOptions(
        ActionBar.DISPLAY_SHOW_TITLE |
        ActionBar.DISPLAY_USE_LOGO |
        ActionBar.DISPLAY_SHOW_HOME |
        ActionBar.DISPLAY_HOME_AS_UP);

This is the setup I have in my current project and never experienced any problems like these.


I had the same problem and I solved it using the following code:

menu.findItem(R.id.menu_search).setOnActionExpandListener(this); 
// Your Activity/Fragment must implement the MenuItem.OnActionExpandListener interface

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    getSherlockActivity().getSupportActionBar().setIcon(R.drawable.uvweb_logo);
    return true;
}

Let me know how this works for you.


I achieved the solution with this simple logic:

public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.menu_search:
            getActionBar().setIcon(R.drawable.your_logo);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

To replace the icon with a logo, specify your application logo in the manifest file with the android:logo attribute, then call setDisplayUseLogoEnabled(true) in your activity.

refer this document1 document2

And please see this answer for more details


Just remove collapseActionView from your menu Xml.


The only thing you need to have set is this (in onCreate of activity):

getSupportActionBar().setIcon(R.drawable.logo);

...not there is also a setLogo.

Regardless to if you have the icon in the manifest set to something different, this will tell the activity to use a different image during runtime.

I tested this with the following menu item and it works:

  <menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/menu_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/search_hint"
          android:orderInCategory="1"
          android:actionViewClass="android.widget.SearchView"
          android:showAsAction="always|collapseActionView"/>

  </menu>

Inflated like normal in onCreateOptionsMenu:

getSupportMenuInflater().inflate(R.menu.search_menu, menu);

I had the same problem, (for ABS) I had:

    ActionBar actionBar = ((SherlockFragmentActivity) activity).getSupportActionBar();
    actionBar.setLogo(R.drawable.logo);

And I just added:

actionBar.setIcon(R.drawable.logo);

And the issue is gone.


I found the best way to solve this problem was to add the SearchView programatically to the menu like this:-

    SearchView searchView = new SearchView(getActionBar().getThemedContext());
    menu.add("Search")
            .setIcon(R.drawable.ic_action_search)
            .setActionView(searchView)
            .setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    getActionBar().setIcon(R.drawable.ic_action_willyweather);

                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    return true;
                }
            })
            .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);

And then use the onActionExpandListener to change the icon/logo to the correct value when then SearchView is expanded.

This solution may also work if you define the SearchView in menu.xml, but you'd then still need to setup the onActionExpandListener() in code to do the same as the above example.

Andy.


Need Your Help

Where do I set defaults for an associated entity in an embedded Symfony2 form?

php symfony2 doctrine2

I have an entity with a OneToOne association to another entity. For these purposes I'll call the initial entity "Parent" and the associated Entity "Child".

How to reference OneNote interop from Visual Studion 2010

com interop ms-office com-interop

I am trying to use the OneNote interop from visual studio 2010. I have OneNote 2010 installed, I am building a .net 2.0 win forms app. I add a reference, Select the COM tab, and choose either

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.