How to theme the actionModeBackground to change the background of the action view searchbar?!?

1,003 views
Skip to first unread message

Peter Arwanitis

unread,
Dec 14, 2012, 1:09:49 PM12/14/12
to actionba...@googlegroups.com
Hi experts,

I've spent now ~6h without any progress on the topic how to change the theme for the searchbar. 
* I call it with onSearchRequested() it pops up like expected in my actionbar
* it *can* change the color, if I change the app theme from  android:theme="@style/Theme.Sherlock" to android:theme="@style/Theme.Sherlock.Light"

From that observation, I concluded, that there has to be a way to control it by app theming through ABS.
But I failed miserably in identification of the source configuration option.

I changed the ninepatch referenced by:
<style name="Theme.Sherlock" parent="Sherlock.__Theme">
         <item name="actionModeBackground">@drawable/abs__cab_background_top_holo_dark</item>

Because it has exactly the colors of the searchbar and I saw a relation to default theme (<sdk>/platforms/android-13/data/res/values/themes.xml):
    <!-- Theme for the search input bar. -->
    <style name="Theme.SearchBar" parent="Theme.Holo.Light.Panel">
        <item name="windowContentOverlay">@null</item>        
        <item name="actionModeBackground">@android:drawable/cab_background_opaque_holo_light</item>
    </style>


But without any effect.

I know, that this is a mixed theming / searchbar / ABS question. 
But maybe someone can shed a light into my dark day :)

regards
Peter
(=PA=)

Jean-Bernard Collet

unread,
Jan 3, 2013, 8:29:26 AM1/3/13
to actionba...@googlegroups.com
Any progress on this topic ?

I'm currently facing the same question.

Thanks

Peter Arwanitis

unread,
Jan 6, 2013, 5:11:57 PM1/6/13
to actionba...@googlegroups.com

Hi,
Yeah we found a solution.
I will have a look at it on Monday if you are still interested.
Lot of other things happened since that day :-)
Regard
Peter

Ch. de la Hulpe 181
1170 Brussels

Image         Image     
Image  Image  Image  Image 
        Image  Image  Image

ATTENTION: The information in this electronic mail message is private and confidential, and only intended for the addressee. Should you receive this message by mistake, you are hereby notified that any disclosure, reproduction, distribution or use of this message is strictly prohibited. Please inform the sender by reply transmission and delete the message without copying or opening it. Messages and attachments are scanned for all viruses known. Always scan attachments before opening them.

Jean-Bernard Collet

unread,
Jan 7, 2013, 2:35:29 AM1/7/13
to actionba...@googlegroups.com
Of course i'm :D

Can you explain me how you did ?

I'm trying to set the search view's background red.

Thanks for your help


Le vendredi 14 décembre 2012 19:09:49 UTC+1, Peter Arwanitis a écrit :

Peter Arwanitis

unread,
Jan 7, 2013, 4:33:18 AM1/7/13
to actionba...@googlegroups.com
Hi Jean-Bernard,

quick summary, I hope it is enough to follow our steps.
Because the relevant stuff is not style-able this technique changes the drawable references during onCreateOptionsMenu().

Feels like Python programming, were you are able to change anything during runtime :)

regards
Peter
(=PA=)

130107 Documentation Search ActionView theming

Steps


Code snippet

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

//      getSupportMenuInflater().inflate(R.menu.activity_home, menu);

 

 

        //Create the search view

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

        //searchView.setQueryHint("Search for countries…");

 

 

        /*

         * Access searchable.xml configuration for SearchView

         */

        SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);

        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());

 

        Log.w(TAG, "====> searchableInfo:" + searchableInfo.getHintId());

        searchView.setSearchableInfo(searchableInfo);

 

 

        /*

         *  Getting id for 'search_plate' - the id is part of generate R file,

         *  so we have to get id on runtime.

         */

 

        // Android SearchView

        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);

        int searchVoiceBtnId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);

        // Alternative access for ABS SearchView

        //int searchPlateId = R.id.abs__search_plate;

 

 

        // Getting the 'search_plate' LinearLayout.

        View searchPlate = searchView.findViewById(searchPlateId);

        // Setting background of 'search_plate' to earlier defined drawable.

        searchPlate.setBackgroundResource(R.drawable.kp_searchview);

 

        /*

         * if existing, align VoiceButton background as well

         */

        Log.w(TAG, "====> searchVoiceBtnId id:" + searchVoiceBtnId);

        if (searchVoiceBtnId != 0) {

            View searchVoiceBtn = searchView.findViewById(searchVoiceBtnId);

            searchVoiceBtn.setBackgroundResource(R.drawable.kp_searchview);

        }

 

        menu.add("Search")

            .setIcon(R.drawable.action_search)

            .setActionView(searchView)

            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

 

 

        return true;

 

    }

 

Kp_searchview.xml

<?xml version="1.0" encoding="utf-8"?>

 

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

    <item android:state_focused="true"

        android:drawable="@drawable/kp_sq_searchviewdefault" />

    <item android:drawable="@drawable/kp_sq_searchviewlostfocus" />

</selector>



Screenshots and nine-patches are attached:

Kp_sq_searchviewdefault.9.png

Kp_sq_searchviewlostfocus.9.png

Result



130107searchbar_customization.png
kp_sq_searchviewdefault.9.png
kp_sq_searchviewlostfocus.9.png

Jean-Bernard Collet

unread,
Jan 7, 2013, 4:40:04 AM1/7/13
to actionba...@googlegroups.com
Thanks for this precise anwser. I will try it asap.

Le vendredi 14 décembre 2012 19:09:49 UTC+1, Peter Arwanitis a écrit :

Jean-Bernard Collet

unread,
Jan 8, 2013, 7:24:55 AM1/8/13
to actionba...@googlegroups.com
Isn't there any other simple solution to set the searchview's background transparent (in fact the same color as the main actionbar) ?

I don't really like to re-implement the whole thing you described if i can afford to.

Now, i just have the following code, and it works fine, but with wrong searchview's background color.

menu.xml :

<item

        android:id="@+id/search"

        android:actionViewClass="com.actionbarsherlock.widget.SearchView"

        android:icon="@drawable/ic_action_search"

        android:showAsAction="always"

        android:title="@string/search"/>


Fragment :

@Override

  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.menu, menu);

  }


  @Override

  public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.search){

      return getSherlockActivity().onSearchRequested();

    }

    return super.onOptionsItemSelected(item);

  }


Le vendredi 14 décembre 2012 19:09:49 UTC+1, Peter Arwanitis a écrit :

Peter Arwanitis

unread,
Jan 10, 2013, 5:23:45 AM1/10/13
to actionba...@googlegroups.com
Hello Jean-Bernard,

sorry for the late answer. I'm not aware of alternative or more simple solution. 
At that point were we found a working solution, we stopped 'optimizing' it, due to other open dev tasks.

And in the end it didn't looked too complicated to me, just intercept some hooks and change the graphics.

sorry that I cannot give you more hints
regards
Peter
(=PA=)


Reply all
Reply to author
Forward
0 new messages