29

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.

enter image description here

enter image description here

akaya
  • 1,140
  • 9
  • 27
  • OP you might want to check the answer by tkeunebr and the related comment. putting that 1 line in my onCreate and my/your problem was gone – Corey Scott Jun 24 '13 at 10:28

8 Answers8

20

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); 
Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • 1
    Changing icon attribute to logo image will change launcher icon. That doesn't help me. – akaya May 20 '13 at 10:38
  • 1
    Did you test this? On which device? I use this solution in my app and it works without affecting the launcher icon on a Galaxy Nexus... – Matthias Robbers May 20 '13 at 11:11
  • Yes you are right, setting icon from style doesn't affect launcher icon. Still search view shows launcher icon instead of logo. I think you may have misunderstood my problem. I will post screenshots to make it clear. – akaya May 20 '13 at 11:40
  • No, the question is clear to me and the answer should be correct. I answered a similar question a while back: http://stackoverflow.com/questions/15982383/how-to-change-dismiss-home-button-of-searchview-in-sherlock-action-bar Just make sure that you set icon and logo to the same drawable. – Matthias Robbers May 20 '13 at 13:10
  • But then again you are missing the whole point of having different icon and logo. – akaya May 20 '13 at 14:26
  • Where do you need another resource for the icon? I thought you were using the logo everywhere. You can set the icon to a different resource in each activity. – Matthias Robbers May 20 '13 at 17:52
  • Worked perfectly for me. – Tim Malseed Oct 28 '13 at 10:54
  • 2
    Make sure to put this `item` in your `ActionBar` style, otherwise it will put a logo on `preferences`. Worked perfectly for me. – theblang Aug 11 '14 at 20:07
  • You save my life. Great answer. Was facing the same issue. – Smeet Jul 08 '15 at 10:48
10

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.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Thanks for the answer Doctoror. I have tried your answer with many combinations, but the output is still the same. Can you confirm that you have different resources for logo and icon in your project and it works? – akaya May 20 '13 at 14:23
  • 1
    Argh, sorry, I used the same icon and did not see the problem. I also found that some other people experienced it and can't do anything about it https://groups.google.com/forum/?fromgroups#!topic/android-developers/r4oJO3DPPis looks like a bug or something undocumented. I tried looking through the source of ABS but I could not find the place it sets the action view expanded, if you'll find it maybe there's the answer. Why can't you use the logo the same as icon for this Activity? – Yaroslav Mytkalyk May 20 '13 at 15:23
7

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.

tkeunebr
  • 266
  • 2
  • 2
  • 1
    This works. Calling getActionBar().setIcon(R.drawable.uvweb_logo) will set both the icon and the logo to be the same image. This is probably a viable option since the OP seems to be starting with the logo. You actually don't need the action expand listener. You can drive this at the activity level in onCreate(). With the call in place, opening search will cause the image to switch to the icon, but the icon is now the same asset as the logo. – javahead76 Jun 17 '13 at 13:06
  • This seems to be a good workaround since it doesn't change the icon in the task manager app where the launcher icon is usually preferable. – wkarl Aug 14 '15 at 09:52
5

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);
    }
Superuser
  • 91
  • 1
  • 4
3

Just remove collapseActionView from your menu Xml.

Simon
  • 10,932
  • 50
  • 49
2

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);
Codeversed
  • 9,287
  • 3
  • 43
  • 42
0

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.

Dante
  • 1,104
  • 1
  • 10
  • 15
0

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.

Andrew Kelly
  • 2,180
  • 2
  • 19
  • 27