Android: Set Width of PopupMenu to Match Parent - android

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.!

Related

How to change a title of popupmenu

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;
}

Anchoring a PopupMenu to an ImageView

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

Add Dropdown view inside a TextView

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();

Show popup menu when action bar item is clicked

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);

Width of the popup menu - How to change

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

Categories

Resources