Godfryd Godfryd - 1 month ago 26
Android Question

How to customize toolbar in Navigation Drawer activity?

After creating a Navigation Drawer activity in my Android Studio project I want to add a search view into the toolbar instead of my app name. How do I do it? Can the toolbar be customized based on my needs or creation of custom toolbar is required?

Navigation Drawer Activity

Answer

If you have selected Navigation Drawer activity while creating new project Android studio would have by default created the file app_bar_main.xml file

You should be able to find the below code given by default

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

Replace the above code with below code to get search view within toolbar

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">

<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content" /></android.support.v7.widget.Toolbar>

enter image description here

Comments