How to make any imagebutton a menu button

Multi tool use
Multi tool use


How to make any imagebutton a menu button



I have an imageButton in a xml file. Now want to make it a menu button, so that when a user click on the button it should show the drop down menus. But I am not able to figure out what are the possible solution(s).
Can anyone help?




2 Answers
2



If you're trying to show a drop down menu when clicking an ImageButton (or any other View), try this:


final ImageButton imageButton = // get your ImageButton from the XML here

final PopupMenu dropDownMenu = new PopupMenu(getContext(), imageButton);

final Menu menu = dropDownMenu.getMenu();
// add your items:
menu.add(0, 0, 0, "An item");
menu.add(0, 1, 0, "Another item");
// OR inflate your menu from an XML:
dropDownMenu.getMenuInflater().inflate(R.menu.some_menu, menu);

dropDownMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
// item ID 0 was clicked
return true;
case 1:
// item ID 1 was clicked
return true;
}
return false;
}
});

imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dropDownMenu.show();
}
});

// if you want to be able to open the menu by dragging on the button:
imageButton.setOnTouchListener(dropDownMenu.getDragToOpenListener());



When Android Studio asks to import PopupMenu you may see two options:





How to customize the color of this pop up menu?
– Ashu_1907
Jul 1 at 21:26





@Ashu_1907 I'm not sure this is the best way but try this (example with blue background and green text): In your themes.xml or styles.xml file add: <style name="MenuTheme" parent="Theme.AppCompat"> <item name="popupMenuStyle">@style/MenuStyle</item> <item name="android:textColor">#00FF00</item> </style> <style name="MenuStyle" parent="Widget.AppCompat.PopupMenu"> <item name="android:popupBackground">#0000FF</item> </style> Then replace new PopupMenu(getContext(), imageButton) with new PopupMenu(new ContextThemeWrapper(getContext(), R.style.MenuTheme) imageButton)
– Gil Castro
Jul 1 at 21:48


<style name="MenuTheme" parent="Theme.AppCompat"> <item name="popupMenuStyle">@style/MenuStyle</item> <item name="android:textColor">#00FF00</item> </style> <style name="MenuStyle" parent="Widget.AppCompat.PopupMenu"> <item name="android:popupBackground">#0000FF</item> </style>


new PopupMenu(getContext(), imageButton)


new PopupMenu(new ContextThemeWrapper(getContext(), R.style.MenuTheme) imageButton)


final ImageButton imageButton ;

final PopupMenu dropDownMenu = new PopupMenu(getContext(), imageButton);

final Menu menu = dropDownMenu.getMenu();
menu.add(0, 0, 0, "Item 1");
menu.add(0, 1, 0, "Item 2");

dropDownMenu.getMenuInflater().inflate(R.menu.some_menu, menu);

dropDownMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:

return true;
case 1:

return true;
}
return false;
}
});

imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dropDownMenu.show();
}
});


imageButton.setOnTouchListener(dropDownMenu.getDragToOpenListener());






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Iuz4mwPaMKP8xnMCDTJxphmSrv40s4,TfV,5D,UXA3XWtOepcK3sqBwL 9e6U
2sSMgrneAYgC iUr7jG l GrcNnTS,u7eBJYEkiQc4iFo2kVHXE2AP6AlSsauZKQCWEo71k9BA

Popular posts from this blog

Rothschild family

Cinema of Italy