Search code examples
javaandroidandroid-layoutandroid-edittexttoolbar

How to add an edittext in the middle of the toolbar?


I want to add an edit text directly to the toolbar. The edittext is always shown below or above the toolbar. What can I do?

I tried to put the edit text in app_bar_nav.xml, but that doesnt help with the position of it. I also tried an include statement and changing the layout type to linear layout... also without a good result.

app_bar_nav.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorBlack"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlack"
        android:hint="Adresse" />

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

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"
    app:srcCompat="@android:drawable/ic_menu_search" />

<include layout="@layout/content_nav" />

activity_nav.xml

<androidx.drawerlayout.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">


<include
    layout="@layout/app_bar_nav"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav"
    app:menu="@menu/activity_nav_drawer" />


Solution

  • Add Edit text inside Toolbar it self, please try below code

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorBlack"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
    
        <EditText
            android:id="@+id/myEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorBlack"
            android:hint="Adresse" />
        </androidx.appcompat.widget.Toolbar>