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();
Related
I have an ImageView that opens a PopupMenu on click. How can I set the PopupMenu to display at a certain position? The PopupMenu will serve as a drop down menu and I would like the top right corner of the PopupMenu to be where the user clicked. See images below for illustration:
What I have currently:
What I want:
The relevant code:
mNavViewDropDown= navViewHeader.findViewById(R.id.navview_header_expand);
mNavViewDropDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(getContext(), mNavViewDropDown);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.menu_navview_header_, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
getContext(),
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
}
});
You can set the gravity of the popup menu.
popup.setGravity(Gravity.LEFT);
That being said, what you are doing is more of a hack. There is no guarantee that it will show up in the left. You should either use an expandableListView or make your own expandable view. Try this library : https://github.com/AAkira/ExpandableLayout. It provides an easy way to show an expandable view.
So instead of using that hacky drop down pop up menu, I realized there is much better tool already made for exactly this:
https://developer.android.com/guide/topics/ui/controls/spinner#java
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();
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.!
How to add a menu popup when an action bar item is clicked (see screenshot)? I want the menu item to show an icon.
tHings I have tried:
Setting actionProvider (support lib v7) for the action bar item. In the actionProvider, return null for onCreateActionView. In onPrepareSubMenu, populate the submenu. This works on Android 2.x but not Android 4.0, and for Android 2.x, there is no icon.
In the actionProvider, create a imageview and on clicking, shows a PopupMenu, but popup menu has no icon, when I have specifically used setIcon to show it.
I don't understand why PopupMenu does not show any icon. I followed the "official" code as closely as possible but to no avail.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/ShareActionProvider.java#195
Please help!
Thanks!
Use popUpMenu ->>> Follow>
res/menu/horario.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_MudaDia"
android:titleCondensed="Mudar Dia"
android:title="Mudar Dia"
android:icon="#drawable/ic_menu_popup"
android:showAsAction="always">
</item>
activity.class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.horario, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_MudaDia:
View vItem = getActivity().findViewById(R.id.menu_MudaDia);
PopupMenu popMenu = new PopupMenu(getActivity(), vItem);
for (int i = 0; i < diaSemana.length; i++)
{
popMenu.getMenu().add(0, i, i, diaSemana[i]);
}
popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
DIA = diaSemana[item.getItemId()];
atualizaGUI();
return true;
}
});
popMenu.show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
You can try creating a layout with the ImageView and TextView. Inflate that layout inside a PopUpWindow (refer : http://developer.android.com/reference/android/widget/PopupWindow.html).
Use the showAsDropDown(View actionBarIcon) method to show the menu on your actionbar icon click.
I use support library v7 and works well.
- use ActionProvider
I use custom ActionProvider and it works well at 2.x and 4.x ,
code in onPrepareSubMenu
subMenu.clear();
// labels contain list item text.
int len = labels.length;
for(int i = 0; i < len; i++) {
subMenu.add(0, labels[i], i, labels[i])
.setIcon(icons[i])
.setOnMenuItemClickListener(new MineMenuItemClickListener());
}
super.onPrepareSubMenu(subMenu);
- about PopupMenu
PopupMenu don't show icon as default, but you can do your own PopupMenu and set icon display.
Like this man does CustomPopupMenu
The only modify is add mPopup.setForceShowIcon(true);
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