Android Google Search Bar with Drawer

2.5k views Asked by At

My app is currently implemented with the Single activity approach (Using navigation architecture component with one main activity and several fragments). I am currently using a toolbar with a drawer.

My app currently look like this:

enter image description here

However in the modern google apps (Google photos, gmail etc..), Google has implemented a new way of navigating using a search field with an implemented drawer in it as shown below:

enter image description here

I want to replace this toolbar with a search bar and the drawer menu exactly like the Google apps.

Can someone help me with some code on how to achieve this?

My main activity is as follows:

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

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/Drawer_Main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.main.MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/Toolbar_Main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <TextView
                android:id="@+id/Toolbar_Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
            android:text="@string/app_name"
                style="@style/Locky.Toolbar.TitleText" />

        </com.google.android.material.appbar.MaterialToolbar>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/Nested_Scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <fragment
                android:id="@+id/Navigation_Host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/navigation_drawer_main" />

        </androidx.core.widget.NestedScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:orientation="vertical">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/ic_account"
            style="@style/Locky.FloatingActionButton.Mini" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/ic_credit_card"
            style="@style/Locky.FloatingActionButton.Mini" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/FAB_Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/fab_margin"
            android:layout_marginEnd="@dimen/fab_margin"
            android:layout_marginBottom="@dimen/fab_margin"
            app:srcCompat="@drawable/ic_add"
            style="@style/Locky.FloatingActionButton.Normal"/>

    </LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/Navigation_View"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:clipToPadding="false"
        app:itemTextAppearance="@style/Locky.TextAppearance.Drawer.Item"
        app:menu="@menu/menu_drawer_main"
        app:headerLayout="@layout/drawer_header"
        style="@style/Locky.Widget.Custom.NavigationView" />

</androidx.drawerlayout.widget.DrawerLayout>

</layout>

Can someone please guide me with some code on how to implement this kind of search bar?

2

There are 2 answers

0
faskN On

I can't tell you how to design that toolbar but this project maybe helps you to handle toolbar's back button and home button with drawer layout.

https://github.com/furkanaskin/Weatherapp/blob/master/app/src/main/java/com/faskn/app/weatherapp/ui/main/MainActivity.kt

2
Hooni On

if you are still looking for a way to implement a gmail style search bar with an app drawer icon, use the toolbar, simply add navigation icon and wrap an editText inside:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:navigationIcon="@drawable/ic_menu">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</androidx.appcompat.widget.Toolbar>

Then set the toolbar in the activity:

val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

And implement the onOptionsItemSelect listener:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // do desired stuff here
    return super.onOptionsItemSelected(item)
}

If you also want to have the floating style and disappearing behavior, wrap the toolbar in an appBarLayout and set app:layout_scrollFlags="scroll|enterAlways|snap":

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="24dp"
    android:layout_marginTop="2dp"
    android:background="@android:color/transparent">
    app:elevation="0dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        app:navigationIcon="@drawable/ic_menu"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"/>
</androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

The following details to mimic the style and the behavior are worth to mention:

appBarLayout

  1. margin horizontal, to have the bar not cover the whole top
  2. background set to a round rectangle shape
  3. margin Top to have a little space
  4. eleveation to 0dp so there is no shadow of the appBarLayout

toolbar

  1. app:contentInsetStartWithNavigation="0dp", otherwise the margin is too big between icon and editText

EditText

  1. background transparent so the underline will disappear

edit: Almost forgot! Very important point: The parent layout has to be CoordinatorLayout, otherwise there will be no reactions to scrolling inside the recyclerview edit2: changed eleveation of the appbar layout to 0dp so there wont be any shadow