Search code examples
androidandroid-actionbaractionbarsherlockandroid-appcompat

AppCompat - AutoCompleteTextView's width not fill parent when used as ActionBar's action view


I was using AutoCompleteTextView, as ActionBar's action view.

It works perfectly fine when I was using SherlockActionBar. Here's the code snippet how I use AutoCompleteTextView, as ActionBar's action view.

main.xml

<menu 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" tools:context=".MainActivity">

    <!-- android.support.v7.widget.SearchView -->
    <item android:id="@+id/action_search"
        android:title="Search me"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="always|collapseActionView"
        app:actionLayout="@layout/collapsible_searchtext" />

    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

collapsible_searchtext.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >
    <android.widget.AutoCompleteTextView
        android:hint="Search stock"
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <requestFocus />
    </android.widget.AutoCompleteTextView>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_marginRight="15dip"
        android:layout_gravity="right|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible" />
</FrameLayout>

As you can see, we are using match_parent in the width properties.

If I were using SherlockActionBar, it looks good as following.

enter image description here

However, if I were using AppCompat with themed Theme.AppCompat.Light.DarkActionBar

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="22" />

The collapsed AutoCompleteTextView's width will not fill parent.

enter image description here

I do have a simple code snippet to demonstrate this problem : https://www.dropbox.com/s/ny4dq0c7ycqe1kc/appcompat-autocompletetextview-issues.zip?dl=0


Solution

  • Change the root FrameLayout in collapsible_searchtext.xml to a RelativeLayout, gets the AutoCompleteTextView to span full width.

    However, I have no idea why it happens so as using FrameLayout does work in SherlockActionBar previously.

    p/s Thanks for Karge's input from reddit