ActionBar / Layouts

Basic ActionBar – Part 5

In the previous article we looked at using different controls in our ActionBar to provide custom behaviour. That’s all well and good, but suppose we actually want to do something a little more complex? In this article we’ll have a look at how we can use custom layouts within our ActionBar.

In Google Search for Jelly Bean, we can see quite a common UI pattern, the addition of an “X” button in an EditText field which allows us to clear the text entered in the control:

One way would be to write a custom control which extends EditText, but there is an alternative which we can achieve by combining standard controls inside a RelativeLayout. Let’s create a new layout in res/layout/search_layout.xml:

[xml]


6 Comments

  1. I think someone should port the current searchView for api level above 11 I guess. Putting an edittext is not the best solution here. If you do that, you have to take care all the rest by yourself.

  2. Thanks again for the tutorials.

    If you just want the standard search widget in the navigation bar, you don’t have to roll your own like this. You can also just specify:

    android:actionViewClass=”android.widget.SearchView”

    in your menu resource.

    1. That will only work for API 11 and later, so not if you’re using ActionBarSherlock.

      Also, I have already covered actionViewClass, and the purpose of this is to demonstrate how to use actionViewLayout.

  3. Nice tutorial and very instructive. In the ADT, there is a new activity with a navigation named “Tabs + Swipe”. Do you think it’s possible to adapt your actionBar tutorial to this new kind of activity ?

    1. It’s not a new Activity, it’s just a wizard which creates a project with some default code. I’ll leave the tutorial as-is because if you understand my tutorial, then using the wizard should be easy; but if I just covered how to use a wizard, you would have no idea of how to customise things once the wizard has done its thing.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.