SearchView shifting AppBar permanently upwards?

222 views Asked by At

I am adding a SearchView to the current view at Runtime, like so:

View searchpage = inflater.inflate(R.layout.search_ui, (ViewGroup) view.getParent(), false);
    SearchView searchbar = (SearchView) searchpage.findViewById(R.id.searchbar);
    //ViewGroup of current layout
    parent.addView(searchbar);

This code is executed when a button is pressed.

This code works, however if the SearchView is entered and exited, the animation that shifts the AppBar upwards works however upon exiting the AppBar is not reset. The SearchView is not embedded in the AppBar, and I am not attempting to do this. I would like the SearchView to be below the AppBar as it is.

The AppBar is defined in a separate xml file. I am switching between multiple views using a ViewPager.

Here are some screenshots:

Before SearchView is Tapped

After SearchView is Tapped/Entered and Exited

The SearchBar is defined in xml like so:

<android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchbar"
        android:layout_below="@+id/view"
        android:iconifiedByDefault="false"
        android:queryHint="Search"
        android:layout_centerHorizontal="true">

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

The AppBar is defined like so:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

Any help would be appreciated, I am at a loss as to why the AppBar won't slide back down automatically.

EDIT: I was concurrently having issues with a set of buttons not appearing at the bottom of the screen. I figured out that they were being drawn underneath the nav bar. I believe that when the SearchView was being entered, the toolbar was being pushed upwards to accommodate for the set of buttons at the absolute bottom of the screen. I added padding to the layout and I believe that was the solution to the issue.

Thanks,

-n.parker

1

There are 1 answers

2
Zac1 On

Try something like this;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical"
tools:context="Your-Activity-name-like-com.xyz.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.SearchView
    android:id="@+id/action_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:title="@string/action_search">
</android.support.v7.widget.SearchView>

And inside the "layout" folder, create a toolbar.xml that looks like this;

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>

Now you can reuse this toolbar in other activities too, id needed.