0

So I am implementing a SearchView and right now I have two items in the layout. The back button and the search view.

I would like to know how can I make sure the back button appears on the left side of the screen before the search view, instead of the opposite.

This is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/back_btn"
        android:title="@string/search"
        app:icon="@drawable/ic_action_back_black"
        android:icon="@drawable/ic_action_back_black"
        app:showAsAction="ifRoom|collapseActionView"/>

    <item android:id="@+id/search"
        android:title="@string/search"
        app:icon="@drawable/ic_search_black_24dp"
        android:icon="@drawable/ic_search_black_24dp"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

I tried to put the back button before the search view, and it works fine until I click on the search box.

The Search view expands and the back button stays on the right side of the screen.

Question : Is there a way to make sure the back button will stay on the left side even when the search box expands?

This is the toolbar I am using:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/PopupMenuStyle"
    android:id="@+id/my_awesome_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/actionBarSize"
    app:titleTextColor="@android:color/black"
    android:layout_alignParentTop="true"
    android:gravity="right"
    android:elevation="5dp">

</android.support.v7.widget.Toolbar>
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50

2 Answers2

1

Hope this will help someone

  • First, make sure your activity not have an ActionBar
 <item name="windowActionBar">false</item>
  • Second, add this code to your .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
        android:id="@+id/my_appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="@dimen/margin_small">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="-9dp"
                android:layout_weight="6" />

            <android.support.v7.widget.SearchView
                android:id="@+id/my_search_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@color/md_white_1000"
                android:clickable="true"
                android:focusable="true"
                android:iconifiedByDefault="false" />
        </LinearLayout>

    </android.support.design.widget.AppBarLayout>
  • Last, set your back arrow programmatically

Display Back Arrow on Toolbar

Dreamers
  • 11
  • 3
0

inside search view tag add this attribute android:maxWidth

Mostafa Anter
  • 3,445
  • 24
  • 26