How can I center an ActionView (a SearchView in particular) inside of a Action Bar?
As seen in the Google Books app:
Google Books app SearchView http://i46.tinypic.com/2z909is.png
My current layout setup (search_layout.xml
):
<?xml version="1.0" encoding="utf-8"?>
<SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:queryHint="@string/search_hint" />
My Action Bar XML file (menu.xml
):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="@+android:drawable/ic_menu_search"
android:title="@string/search"
android:showAsAction="always|collapseActionView"
android:id="@+id/searchMenuItem"
android:actionLayout="@layout/search_layout" />
</menu>
Is there a way to mimic the behaviour of the Books' SearchView?
Have a look here. You can just use the android:actionViewClass="android.widget.SearchView" attribute, and that should work just fine (keep in mind that you can get a reference to the searchView in Java, and change the hint there if that's what you want).
A
View
centered in theActionBar
can't be achieved withAction Items
in anxml menu
, as far as I can tell. However it can be implemented by setting a custom layout to theActionBar
. A basic example:An
Activity
:where
actionbar_layout
is:creates this on a 7'' tablet:
Note the
iconifiedByDefault
attribute set tofalse
on theSearchView
which forces it to appear expanded and not just as an icon. This may require that you hide the softkeyboard programmatically or it will be launched open when the Activity starts.