Question

I want to show an AutoCompleteTextView that is always expanded in my ActionBar. This is my menu xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/visibility_menu_search"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="always"
        android:actionLayout="@layout/visibility_search" />
</menu>

This is my layout/visibility_search.xml

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/visibility_search_auto_complete_text_view" />

This is my Activity:

public class VisibilityActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(layout.visibility_activity);
        getActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.visibility, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

I'm using showAsAction="always" because I want the AutoCompleteTextView to always be visible (it will be the only thing in the ActionBar).

My AutoCompleteTextView is always right-aligned in the ActionBar and only has 100px width. Why is this?

Update

Thanks to Atul O Holic's answer, I found a preferred solution for my case, which doesn't require any special logic at all. I'm leaving my answer as correct because it works for the general case, but for my specific case, a SearchView is better and easier.

Was it helpful?

Solution

theelfismike's answer pointed me in the right direction, but I was able to simplify it a bit. The answer he linked to referred to displaying a custom view with ActionBar#setCustomView, whereas I'm trying to make an action view set with MenuItem#setActionView take up the full width. It is possible that my simplification won't work well for ActionBar custom views.

This is the xml that worked for me:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
    <EditText xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/visibility_search_edit_text"/>
</RelativeLayout>

Unfortunately, this hides the ActionBar's title. An answer in the question theelfismike linked to referred to this problem, but I couldn't get the suggested workaround to work.

Also, RelativeLayout seems to be the required root of an action view. I tried using a LinearLayout with many different combinations of layout_gravity and layout_width, but I couldn't get a LinearLayout to fill the full width.

OTHER TIPS

Checkout this solution: How to display custom view in ActionBar?

I think I had to do something similar.

I think the magic parts are that it has to be a LinearLayout inside a RelativeLayout and have this attribute set: android:layout_gravity="fill_horizontal"

I agree with @Emmanuel in context of using SearchView as it's an in-built View and is meant to Search. :) It also goes along well specially with ActionBar.

I want it to be expanded all the time.

Adding the below will always keep it expanded by default (unless user himself closes it),

searchView.setIconified(false);

We can also add auto-suggestions to SearchView via a ContentProvider. Here's a doc from Google on the same. You may also like this.

Give it a shot. Have Fun. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top