Android Context Menu

Context Menu biasanya suka nongol di pojok kanan atas aplikasi,

android-context-menu

atau bisa juga bentuknya seperti popup

download

saya lagi nyari digoogle tentang Context Menu, ternyata ada library Context Menu yang lumayan bagus.

langsung saja deh saya kasih tahu caranya

  1. install library via gradle
    gradle-yalantis
  2. membuat List Object
private List<MenuObject> getMenuObjects() {
    // You can use any [resource, bitmap, drawable, color] as image:
    // item.setResource(...)
    // item.setBitmap(...)
    // item.setDrawable(...)
    // item.setColor(...)
    // You can set image ScaleType:
    // item.setScaleType(ScaleType.FIT_XY)
    // You can use any [resource, drawable, color] as background:
    // item.setBgResource(...)
    // item.setBgDrawable(...)
    // item.setBgColor(...)
    // You can use any [color] as text color:
    // item.setTextColor(...)
    // You can set any [color] as divider color:
    // item.setDividerColor(...)

    List<MenuObject> menuObjects = new ArrayList<>();

    MenuObject close = new MenuObject();
    close.setResource(R.drawable.icn_close);

    MenuObject send = new MenuObject("Send message");
    send.setResource(R.drawable.icn_1);

    MenuObject like = new MenuObject("Like profile");
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.icn_2);
    like.setBitmap(b);

    MenuObject addFr = new MenuObject("Add to friends");
    BitmapDrawable bd = new BitmapDrawable(getResources(),
            BitmapFactory.decodeResource(getResources(), R.drawable.icn_3));
    addFr.setDrawable(bd);

    MenuObject addFav = new MenuObject("Add to favorites");
    addFav.setResource(R.drawable.icn_4);

    MenuObject block = new MenuObject("Block user");
    block.setResource(R.drawable.icn_5);

    menuObjects.add(close);
    menuObjects.add(send);
    menuObjects.add(like);
    menuObjects.add(addFr);
    menuObjects.add(addFav);
    menuObjects.add(block);
    return menuObjects;
}

3. Melakukan pemanggilan fragment

private void initMenuFragment() {
    MenuParams menuParams = new MenuParams();
    menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
    menuParams.setMenuObjects(getMenuObjects());
    menuParams.setClosableOutside(false);
    mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    mMenuDialogFragment.setItemClickListener(this);
    mMenuDialogFragment.setItemLongClickListener(this);
}

4. Meletakkan Button pada Context Menu

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.context_menu:
            if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}

5. Membuat Context Menu supaya bisa diklik

@Override
public void onMenuItemClick(View clickedView, int position) {
    Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onMenuItemLongClick(View clickedView, int position) {
    Toast.makeText(this, "Long clicked on position: " + position, Toast.LENGTH_SHORT).show();
}

yah begitulah yang harus ditambahkan di class MainActivity, kalo mau yang lengkap silahkan download disini linknya

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s