I want to view the back arrow behind the SearchView. https://i.sstatic.net/4iBxb.png XML:
<item android:id="@+id/search"
android:title="@string/search_btn_hint"
android:icon="@drawable/ic_action_search"
android:titleCondensed="@string/search_btn_hint"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
I tried to use "collapseActionView|ifRoom" but it didn't give the focus to the SearchView. My java code to give the focus:
getMenuInflater().inflate(R.menu.search, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setQueryHint("Search...");
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
Just add a toolbar to your .xml layout file (it's a Relative Layout in my case, but that's not a demand. Could be a Linear Layout or whatever, just what you're used to use inside your activities layout):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Background"
tools:context=".YourActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/YOUR_TOOLBAR_ID_HERE"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/Header"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:textSize="20sp">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
Instantiate your activity's layout inside OnCreate():
setContentView(R.layout.your_activity_layout);
And do something like this, right after instantiating the layout:
mToolbar = (Toolbar) findViewById(R.id.YOUR_TOOLBAR_ID_HERE);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(null);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
add this to your searchview inside your java class
searchView.setIconifiedByDefault(false);
and finally, the xml searchable menu item:
<item
android:id="@+id/action_searchable_activity"
android:orderInCategory="1"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/title_activity_searchable"
app:showAsAction="ifRoom"
android:iconifiedByDefault="false"
app:actionViewClass="android.support.v7.widget.SearchView"/>
It works for me. Hope it helps.