Read More
Read now

Change the text colors and icons of searchview in action bar.

Save

In this blog, we will learn on how to change the text colors and icons of the searchview in action bar.

We all inflate some menu in all our activities to make our app user interactive.
While working in the same manner, I used the search item but then after sometime i needed to change the same to an expanded search view along with all the other action icons present.

One approach was to make the toolbar and inflate in every activity, but that would be very cumbersome.
So i decided to look up and experiment with the search view that i created in”onCreateOptionsMenu” method.

And finally i got success.

All you need to do is to override the default resources associated with the search view which in turn are picked as per the styles.xml file or default theme applied in your application.

Yes , of course you can create the style attribute for search view and apply it to all the search views you want to.

But i did it with java.
STEPS TO FOLLOW :

  1. Expand the search view by setting iconified and iconifiedByDefault flags to false.
  2. Find views inside the search view and set the colors as per your wish.
  3. Find views of the icons you wish to change and replace them with your icons.

And it’s done.
Lets have a look at the code :

That’s all, check it out.

Keep coding, Keep sharing : )

 

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • Sagar Balyan
    Line #22 throws Null Pointer Exception. Doesn’t work at all !!
    • anchit (Moderator)
      Hello Sagar,
      Thanks for your comment.

      Can you please let me know if you have enabled the voice search for your search view before commenting that this code is giving null pointer exceptiion ??

      Also, if you can show me the code you are using then perhaps I would be able to help you.
      Waiting for your reply.

  • Saif
    Throws this exception { java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setImageResource(int)’ on a null object reference }

    Dose not work!

    • anchit (Moderator)
      Hi Saif,

      This statement implies that you. are referring to the image view before it is being rendered and hence is null.

      Please do cross check your code once.

      The null pointer exception has nothing to do with the code/ approach mentioned in the blog.

      Please do share where exactly are you using this code and i will help you out in getting that part fixed.

      Regards,

  • css.php

    Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!

    Deniel Kerr

    Owner and Founder. Opencart

    Talk to Sales
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home