Toolbar custom layout on Android

The bug occured when I switched from the old ActionBar to the new Toolbar.

I have an activity in which I’m allowing user to click on an icon in the toolbar, and then a search box appears (in the toolbar), using custom layout.

Problem is, it worked fine when I used just Action Bar, but now after changing to Toolbar with all the new layout of AppBarLayout and CoordinatorLayout, once I click the button and switch to the custom action bar layout with the search box, the layout spans on all screen, instead of the normal size of the toolbar.

Here’s the code I’m using to replace the toolbar:

LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflator.inflate(R.layout.map_action_layout, null);

actionBar = ((DrawerActivity)getActivity()).getSupportActionBar();
actionBar.setCustomView(actionBarView);

Here’s the custom toolbar layout I want to put instead:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
    android:id="@+id/autoComplete"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:hint="@string/search_adress_hint"
    android:imeOptions="actionSearch"
    android:inputType="textAutoComplete|textAutoCorrect"
    android:paddingRight="50dp"
    android:textColor="@android:color/white"
    android:textCursorDrawable="@null" >
    <requestFocus />
</AutoCompleteTextView>

<ImageButton
    android:id="@+id/clear_address"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="17dp"
    android:background="@drawable/ic_action_cancel"
    android:gravity="center_horizontal|center_vertical"
    android:scaleType="fitCenter" />

</RelativeLayout>

Note: no matter what value I put in the height of the above, whether it’s fixed dp value, wrap_content, or “?attr/actionBarSize”, the layout still spans on whole screen.

Here’s my toolbar layout being replaced:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_scrollFlags="scroll|enterAlways"
    android:elevation="4dp"
    app:layout_collapseMode="pin">

</android.support.v7.widget.Toolbar>

The toolbar resides inside an AppBarLayout:

 <android.support.design.widget.CoordinatorLayout
        android:id="@+id/top_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/app_bar">
            <include
                android:id="@+id/tool_bar"
                layout="@layout/tool_bar"/>
            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:layout_below="@id/app_bar"/>

        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>

Does anyone have an idea what am I doing wrong? How can I achieve the result I’m looking for? Thanks.


Source: java

Leave a Reply