Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android appcompat toolbar stretches when searchview gets focus

Tags:

I'm updating an app to use the new Toolbar instead of the regular ActionBar. In my app the user must be able to select a contact from their contact list. To do so, I've added a SearchView to the menu. The contacts are already in the list; the SearchView is used to filter the list using the ArrayAdapter.getFilter() method.

It all worked fine using the ActionBar, but the Toolbar's height gets stretched to just behind the keyboard. Using the XML inspection from Android Device Monitor I can see the ListView exists behind my keyboard.

It almost seems as if the SearchView wants to display suggestions, but I have no such thing configured. Any idea what's going wrong?

The images illustrate the problem. They show the normal, expanded and focused state of the SearchView.

This is my XML menu:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">     <item android:id="@+id/action_forwarding_contact_search"         app:showAsAction="always"         android:title="@string/forwarding_contact_search"         app:actionViewClass="android.support.v7.widget.SearchView"         android:icon="@drawable/abc_ic_search_api_mtrl_alpha"/> </menu> 

Edit Giving the Toolbar a fixed height doesn't solve the problem, because it will make SearchView disappear when it has focus. Changing the gravity of either item seems to have no effect on the SearchView.

Normal stateExpanded stateFocused state

like image 439
ar34z Avatar asked Jan 20 '15 10:01

ar34z


1 Answers

I had the same problem than OP, I tried the android:fitsSystemWindows="true" solution, but it was half resolved: the searchview didn't expand anymore but the notification bar (top of screen) became totally white (like the layout background) instead of red (my app theme).

I found an alternative way and it's working like a charm, so for those who are stuck, try this:

In your manifest, just add this line in your activity section:

android:windowSoftInputMode="adjustPan"

Example:

<activity     android:name=".MainActivity"     android:windowSoftInputMode="adjustPan"     android:label="My main activity" > </activity> 
like image 197
Rob Avatar answered Sep 25 '22 09:09

Rob