Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User defined ActionBar Action View: getting the width right

On Ice Cream Sandwich:

I'm looking to add an AutoCompleteTextView to an ActionBar through the standard Action View mechanism (because SearchView isn't available pre-ICS and I'm also using ActionBarSherlock):

<item android:id="@+id/menu_search" android:actionViewClass="com.example.AutoCompleteActionView" android:showAsAction="ifRoom" android:title="@string/address"></item>
<item android:id="@+id/menu_close" android:icon="@drawable/ic_menu_close_clear_cancel" android:showAsAction="always"></item>
<item android:id="@+id/menu_ok" android:icon="@drawable/ic_menu_ok" android:showAsAction="always"></item>

This works, however by default it does not consume the available space in the ActionBar, which I would like.

I've looked at the source for the SearchView and seen how it overrides onMeasure, and done the same thing for my own class that I derived from AutoCompleteTextView. When I do this, the AutoCompleteTextView consumes all the space, leaving no space for two menu items I want to display to the right of it.

It looks as though the width returned from MeasureSpec.getSize() does not take into account the other two menu items when the MeasureSpec.getMode() is MeasureSpec.AT_MOST.

Anyone done anything similar? Any suggestions?

Thanks, Damian

like image 485
Damian Avatar asked Feb 14 '12 11:02

Damian


People also ask

What does the onOptionsItemSelected() method receives as a parameter?

If an action is selected, the onOptionsItemSelected() method in the corresponding activity is called. It receives the selected action as parameter.

What is ATTR actionBarSize?

In specific, android:layout_marginTop="? android:attr/actionBarSize" means: "the size (height) of the action bar".

What is android Action bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

I think this could help:

Create a collapsible menu item with a custom layout:

<?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:id="@+id/menuSearch"
    android:showAsAction="always|collapseActionView" android:icon="@drawable/action_search" android:actionLayout="@layout/collapsible_absearch">
  </item>
</menu>

This is the custom layout:

<?xml version="1.0" encoding="utf-8"?>
 <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ab_Search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search" />

Then in your activity:

public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activitymenu, menu);

    MenuItem menuItem=menu.findItem(R.id.menuSearch);

    menuItem.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            mAbSearch=(AutoCompleteTextView) item.getActionView().findViewById(R.id.ab_Search);

            // Set your adapter and do whatever you want

            return true;
        }
    });
like image 92
stepic Avatar answered Oct 12 '22 23:10

stepic


I wasn't able to get Matthias's suggestion to work in code (maybe I was missing something), but adding a minWidth attribute to the topmost element in my Action View did the trick.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="64dip"
    android:minWidth="64dip"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_gravity="center"
        android:id="@+id/imageViewAnimatedProgressSpinner"
        android:background="@drawable/animated_progress_spinner" />

</FrameLayout>
like image 27
inky Avatar answered Oct 12 '22 23:10

inky