akaya
akaya

Reputation: 1140

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.

enter image description here

enter image description here

Upvotes: 29

Views: 6413

Answers (8)

Andrew Kelly
Andrew Kelly

Reputation: 2188

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.

Upvotes: 0

Superuser
Superuser

Reputation: 91

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);
    }

Upvotes: 5

Dante
Dante

Reputation: 1094

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.

Upvotes: 0

Codeversed
Codeversed

Reputation: 9473

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);

Upvotes: 2

tkeunebr
tkeunebr

Reputation: 266

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.

Upvotes: 7

Simon
Simon

Reputation: 11190

Just remove collapseActionView from your menu Xml.

Upvotes: 3

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17105

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.

Upvotes: 10

Matthias Robbers
Matthias Robbers

Reputation: 15728

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); 

Upvotes: 20

Related Questions