Recently i've been getting a lint error on my usage of android.support.v7.view.menu.MenuPopupHelper which is now hidden and restricted to be only used within its library group.
Exact message:
MenuPopupHelper constructor can only be called from within the same library group (groupId=com.android.support)
Excerpt from the MenuPopupHelper.java class:
/**
* Presents a menu as a small, simple popup anchored to another view.
*
* #hide
*/
#RestrictTo(LIBRARY_GROUP)
public class MenuPopupHelper implements MenuHelper {
Question:
Any idea when and why this happened ? or whats the workaround that I should look for ?
I just found out here that this is a bug in the pre-release version of the tool.
If you want a project wide workaround, put the snippet below in the build.gradle file of your project
android {
lintOptions {
disable 'RestrictedApi'
}
}
OR
use the annotation below to suppress the lint warning for that particular method or class
#SuppressLint("RestrictedApi")
I had an error in Lint report:
Error: MenuPopupHelper.show can only be called from within the same
library group prefix (referenced groupId=androidx.appcompat with
prefix androidx from groupId=MyProject) [RestrictedApi]
}.show()
If you don't use icons in PopupMenu, you can remove MenuPopupHelper:
Before:
val popupMenu = PopupMenu(context, v)
...
val menuHelper = MenuPopupHelper(context, popupMenu.menu as MenuBuilder, v)
menuHelper.gravity = Gravity.END
menuHelper.show()
After:
val popupMenu = PopupMenu(context, v)
...
popupMenu.gravity = Gravity.END
popupMenu.show()
Or add #SuppressLint("RestrictedApi") annotation.
I had the same problem when I migrated to AndroidX and I replaced the following classes:
android.support.v7.widget.PopupMenu
android.support.v7.view.menu.MenuPopupHelper
with the new ones:
androidx.appcompat.widget.PopupMenu
androidx.appcompat.view.menu.MenuPopupHelper
and I obtained the same lint error.
After looking to the reference docs, I discovered that now I can obtain the same behavior using just PopupMenu, without the need of MenuPopupHelper, even if my popup menu has icons.
PopupMenu popup = new PopupMenu(getActivity(), view);
popup.getMenuInflater().inflate(R.menu.opzioni_programma, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_programma_calendar:
// menu item logic
case R.id.action_programma_search:
// menu item logic
default:
return false;
}
}
});
popup.setForceShowIcon(true);
popup.show();
Try using android.support.v7.widget.PopupMenu instead:
PopupMenu popup = new PopupMenu(v.getContext(), v);
popup.inflate(R.menu.mymenu);
//or
//popup.getMenuInflater().inflate(R.menu.mymenu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
final int itemId = item.getItemId();
switch (itemId) {
case R.id.someid:
//do something
return true;
default:
return false;
}
}
});
popup.show();
Related
I have a problem with changing of popupmenu title.
My goal is that there is a join menu in popupmenu list.
After user join the app by using the popupmenu button , I want to change the "join" title to "User profile".
But I don't know how to change the title of popupmenu.
If there has a solution, let me know how to change.
Here is the code
<item android:id="#+id/menu6"
android:title="join"/>
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_menu://popupbutton
PopupMenu popup = new PopupMenu(getApplicationContext(), v);
getMenuInflater().inflate(R.menu.main_menu, popup.getMenu());
popup.setOnMenuItemClickListener(popupClick);
popup.show();
}
PopupMenu.OnMenuItemClickListener popupClick = new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuitem) {
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
break;
}
You can follow this answer
Change PopupMenu items' title programatically
.
First create a boolean variable
private boolean menu6;
Create a menu object to check which popup item is clicked
Menu menuOpts = popup.getMenu();
if (menu6) {
menuOpts.getItem(1).setTitle("User profile");
}
Modify onMenuItemClick to this
switch (menuitem.getItemId()) {
case R.id.menu6: // here is a code of join
menu6 = true
break;
}
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?
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:
android.support.v7.widget.PopupMenu this is the best option, it ensures your menu will work in any Android version
android.widget.PopupMenu this one only works on Android 2.1 and up, which is probably fine. However, if new Android versions come with new features in PopupMenu, the first option may also let you use those new features in older Android versions.
I am trying to implement a custom drop down menu using PopupMenu.
I have set a null key listener to the edit text and onclick of the edit text the PopupMenu appears. I have used the following code to implement PopupMenu.
mStatusEt.setKeyListener(null);
#OnClick(R.id.newProsStatusEt)
public void onMStatusEtClicked() {
PopupMenu popupMenu = new PopupMenu(AddProspectActivity.this, mStatusEt);
popupMenu.getMenuInflater().inflate(R.menu.status_popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
mStatusEt.setText(item.getTitle());
return true;
}
});
popupMenu.show();
}
Everything is working fine but the width of PopupMenu doesn't match the width of its parent.
Any kind of help will be appreciated. Thanks.!
I have to create a layout where i need to display a paragraph on a TextView. Certain words in that paragraph need to be displayed inside a drop down view. Where do i get started with this ?
Kind Regards
If i understand you right - Like joao2fast4u said you can use Spinner or PopupMenu.
example with popupMenu:
PopupMenu popup = new PopupMenu(this, someButton);
popup.getMenuInflater().inflate(R.menu.your_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// switch your menu item and do something..
return true;
}
});
popup.show();
I am using the following code for displaying pop up menus
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
But the menu is covering a lot of my screen area. How to specify the size of the popup menu ?
You'll have to create a custom Dialog. See the official docs on how to do this here:
https://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog