Add awesome animated context menu to your app
ContextMenu
You can easily add awesome animated context menu to your app.
Usage:
For a working implementation, have a look at the app
module
1. Clone repository and add sources into your project or use Gradle:
compile 'com.yalantis:contextmenu:1.0.7'
2. Create list of MenuObject
, which consists of icon or icon and description.
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(...)
Now You can easily add text appearance style for menu titles:
In your project styles create style for text appearance
(For better visual effect extend it from TextView.DefaultStyle):
<style name="TextViewStyle" parent="TextView.DefaultStyle">
<item name="android:textStyle">italic|bold</item>
<item name="android:textColor">#26D0EB</item>
</style>
And set it's id to your MenuObject :
MenuObject addFr = new MenuObject("Add to friends");
BitmapDrawable bd = new BitmapDrawable(getResources(),
BitmapFactory.decodeResource(getResources(), R.drawable.icn_3));
addFr.setDrawable(bd);
addFr.setMenuTextAppearanceStyle(R.style.TextViewStyle);
You can set any color
as divider color:
item.setDividerColor(...)
Example:
MenuObject close = new MenuObject();
close.setResource(R.drawable.icn_close);
MenuObject send = new MenuObject("Send message");
send.setResource(R.drawable.icn_1);
List<MenuObject> menuObjects = new ArrayList<>();
menuObjects.add(close);
menuObjects.add(send);
3. Create newInstance
of ContextMenuDialogFragment
, which received MenuParams
object.
MenuParams menuParams = new MenuParams();
menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
menuParams.setMenuObjects(getMenuObjects());
menuParams.setClosableOutside(true);
// set other settings to meet your needs
mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
4. Set menu with button, which will open ContextMenuDialogFragment
.
@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:
mMenuDialogFragment.show(fragmentManager, "ContextMenuDialogFragment");
break;
}
return super.onOptionsItemSelected(item);
}
5. Implement OnMenuItemClickListener
interface with onMenuItemClick
method.
public class MainActivity extends ActionBarActivity implements OnMenuItemClickListener
…
@Override
public void onMenuItemClick(View clickedView, int position) {
//Do something here
}
…
mMenuDialogFragment.setItemClickListener(this);