Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

couldn't change actionbarsherlock SearchView icon

When I use following configuration:

<item
    android:id="@+id/menu_item_search"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"        
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom"
    android:title="@string/action_search"/>

ActionBarSherlock SearchView doesn't react on provided android:icon

but in case of

android:showAsAction="ifRoom|collapseActionView"

icon is changed correctly but close button inside search view is absent.

How to change search icon and show close button in the right search view corner ?

This is my AppTheme:

<style name="AppTheme" parent="AppBaseTheme">               
    <item name="android:windowBackground">@color/background_window</item>
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <item name="homeAsUpIndicator">@drawable/new_indicator</item>
    <item name="android:homeAsUpIndicator">@drawable/new_indicator</item>

    <!-- ActionBar (SearchView) fix -->
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
    <item name="searchDropdownBackground">@drawable/abs__search_dropdown_dark</item>
    <item name="searchViewTextField">@drawable/abs__textfield_searchview_holo_dark</item>
    <item name="searchViewTextFieldRight">@drawable/abs__textfield_searchview_right_holo_dark</item>        
    <item name="searchViewCloseIcon">@drawable/abs__ic_clear</item>
    <item name="searchViewSearchIcon">@drawable/abs__ic_voice_search</item>
    <item name="searchViewGoIcon">@drawable/abs__ic_go</item>
    <item name="searchViewVoiceIcon">@drawable/abs__ic_voice_search</item>
    <item name="searchAutoCompleteTextView">@style/Widget.Sherlock.SearchAutoCompleteTextView.DarkActionBar</item>         
</style>

<style name="Widget.Sherlock.SearchAutoCompleteTextView.DarkActionBar">
    <item name="android:textColor">?attr/textColorPrimaryInverse</item>
</style>

application from manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

actionbar menu:

<item
    android:id="@+id/menu_item_search"
    android:icon="@drawable/abs__ic_clear"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView"        
    android:showAsAction="ifRoom"
    android:title="@string/action_search"/>    
<item
    android:id="@+id/menu_item_contact"
    android:icon="@drawable/ic_action_user"
    android:showAsAction="ifRoom"
    android:title="@string/action_contact"/>  

SearchView menu configuration:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();

    if (null != searchView) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);         
    }

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            // adapter.getFilter().filter(newText);
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            // this is your adapter that will be filtered
            // adapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
like image 968
alexanoid Avatar asked Mar 24 '23 16:03

alexanoid


1 Answers

You can accomplish this by setting an attribute for your activity theme. In your styles.xml add the following lines and make sure to apply MyTheme or whatever you want to call it to either your application or the activity where your search view lives. I've also added the "searchAutoCompleteTextView" attribute which will allow you to customize the text color and such if you would like to.

    <style name="MyTheme" parent="Theme.Sherlock">
            <item name="searchViewCloseIcon">@drawable/YOUR_CLOSE_ICON</item>
            <item name="searchViewSearchIcon">@drawable/YOUR_SEARCH_ICON</item>
            <item name="searchAutoCompleteTextView">@style/searchTextView</item>
    </style>

    <style name="searchTextView" parent="Widget.Sherlock.SearchAutoCompleteTextView">
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textColorHint">@android:color/black</item>
    </style>
like image 118
Bobbake4 Avatar answered Mar 31 '23 13:03

Bobbake4