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