androidmenuandroid-actionbarcustom-contextmenu

showing contextual action bar at other position


I am using a gridview of photos. Long pressing a photo will start to count the number of selected photos. Coding as follows:

Coding:

gd_view.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
            {
                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu)
                {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                       // TODO Auto-generated method stub

                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return true;
                }

                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
                {
                    int selectCount = gd_view.getCheckedItemCount();
                    switch (selectCount)
                    {
                        case 1:
                            mode.setSubtitle("1 item selected");
                            break;
                        default:
                            mode.setSubtitle("" + selectCount + " items selected");
                            break;
                    }
                }

                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu)
                {

                    mode.setTitle("Select Items");
                    mode.setSubtitle("1 item selected");
                    return true;
                }
            });

Question:

Instead of showing contextual action bar at the top, can I inflate a custom menu at any desired location, e.g. bottom of my app so as to select / dis-select items ? I would like to share the selected photos afterwards.


Solution

  • You can do that by using support Toolbar like this:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    

    You can place this toolbar anywhere in your activity's XML file.

    You need to set this toolbar as action bar in your activity like this:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        }
    }