Anchoring a PopupMenu to an ImageView - android

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

Related

Customizing the popup menu for displaying it's contents on the top of it's button Click?

I have created multiple buttons in my main activity, on click of each button a toast message would be displayed and on Long-click of each button, a popup-menu would be displayed.
As normal popup-menu displays the contents in drop-down format, but I want the popup menu contents to be displayed on Top of the button once the button is Long pressed. I have used menu.XML file to store the items for the popup menu.
Here is the CODE:
public boolean onLongClick(View v) {
#SuppressLint("ResourceType")
PopupMenu popup = new PopupMenu(this, v,Gravity.TOP);
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
switch (v.getId()) {
case R.id.btn1:
popup.show();
break;
case R.id.btn2:
popup.show();
break;
case R.id.btn3:
popup.show();
break;
case R.id.btn4:
popup.show();
break;
}
return false;
}
}
I have gone through many sites but most of them all are customized only by adding icons to the popup menu.
So Please, do let me know some of the ways to find the solution to it.
Thank you

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

Android: Set Width of PopupMenu to Match Parent

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

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

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