Building ActionMode with custom layout

1,463 views
Skip to first unread message

Shem Magnezi

unread,
Jun 2, 2012, 10:51:18 AM6/2/12
to actionba...@googlegroups.com
I just starting using ActionBarSherlock for building some simple app,
in my first screen I have simple list and I added new menu item for adding new item to the list:

    MenuItem newItem = menu.add("New");
    newItem.setIcon(R.drawable.ic_compose_inverse)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

now when user choose to add a new item I want to start a new action mode for adding new item, this action mode should contain a simple layout with text box and a button, so I created this layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
            <EditText
                android:id="@+id/text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text" >
            </EditText>
            <Button
                android:id="@+id/addBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add" />
    </LinearLayout>
so now I just need to set this layout to the bar in the new action mode:

    newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
    actionMode = startActionMode(new MyAction(ListEditor.this));
    return true;
    }
    });

and in my action:

    private final class MyAction implements ActionMode.Callback {
        ...
    @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    View customNav = LayoutInflater.from(context).inflate(R.layout.add_item, null);
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
            return true;
        }
    }

So basically I need something between ActionModes and CustomNavigation from the sherlock example, but the problem is that it set the layout to the main bar and not for the new bar that open in for action.

any suggestions?

Jake Wharton

unread,
Jun 2, 2012, 3:55:43 PM6/2/12
to actionba...@googlegroups.com
Can't be done with the current API. Feel free to file a feature request on http://b.android.com and perhaps it'll make it into the next version of Android.

---
Jake Wharton
http://about.me/jakewharton
Reply all
Reply to author
Forward
0 new messages