Multiple Popup Menus - android

I want to use popup menu for two(2) buttons. The way I am achieving it right now is by making two separate xml files popup_menu1.xml and popup_menu2.xml which are inflated for each button, button1 and button2 respectively.
Popup-menu-1 has two(2) menu items and popup-menu-2 has four(4) menu items.
The pics are added for more clarification.
popup_menu1.xml has two menu-items, a seperate xml-file
popup_menu2.xml has four menu-items, a separate xml-file
each file is inflated for individual buttons.
My Question is: Can I use(inflate) only one(1) xml-file, instead of two separate xml-files, for two(2) buttons and showing two(2) different popup_menus?
Any help is appreciated.
Thank you,
P.S: I wanted to add pictures for more clarity, but as a new member they are not allowing me to do it.
Code Added:
popup_menu1: File1
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu001">
<group android:id="#+id/group_popupmenu">
<item android:id="#+id/menu1"
android:title="Today's Date"/>
<item android:id="#+id/menu2"
android:title="Custom Date"/>
</group>
</menu>
popup_menu2: File2
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu002">
<group android:id="#+id/group_popupmenu">
<item android:id="#+id/menu1"
android:title="Last Seven (07) Days"/>
<item android:id="#+id/menu2"
android:title="Today"/>
<item android:id="#+id/menu003"
android:title="Yesterday"/>
<item android:id="#+id/menu4"
android:title="Last Twenty Eight (28) Days"/>
</group>
</menu>
Edit version 1:
Code In Activity Class:
// add a click listener to the first button
startDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
PopupMenu popup = new PopupMenu (CampaignDetailsActivity.this, view);
popup.getMenuInflater().inflate(R.layout.popup_menu_01, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.menu1:
// some code here
case R.id.menu2:
// some code here
}
return true;
});
popup.show();
}
});
// add a click listener to the end date button
endDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
PopupMenu popup = new PopupMenu (TestAdlikelyButtonsAndMenuActivity.this, button);
popup.getMenuInflater().inflate(R.layout.popup_menu2, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.menu1:
// some code here
case R.id.menu2:
// some code here
case R.id.menu003:
// some code here
case R.id.menu4:
// some code here
}
return true;
}
});
popup.show();
}
});
Images:

Yes, you can inflate a single XML file multiple times, but in the example you gave, you're doing it right by having two XML files. After inflating, but before calling PopupMenu.show(), you can use one of the variants of Menu.add() to add items to the Menu (the one returned by PopupMenu.getMenu()).

Related

open popup on long click of action bar

I want to show the popup menu on longer click of action bar but exactly below where I click.
toolbar.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
PopupMenu popupMenu = new PopupMenu(HomeActivity.this,toolbar);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.device_setting:
Toast.makeText(HomeActivity.this, "dev settings", Toast.LENGTH_SHORT).show();
break;
case R.id.app_update:
break;
case R.id.wireless_setting:
break;
case R.id.hide_footer:
break;
}
return true;
}
});
popupMenu.show();
return true;
}
});
and popup_menu.xml is below
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/device_setting"
android:title="Device Setting"></item>
<item android:id="#+id/app_update"
android:title="App Update"></item>
<item android:id="#+id/wireless_setting"
android:title="Wireless Setting"></item>
<item android:id="#+id/hide_footer"
android:title="Full Screen"></item>
</menu>
screen shot is given below.
I want to open popup exactly below where I click (longer click)on ActionBar. Please help
Image Screenshot
According to your requirement, you just need to use the popup window because is very flexible, I prefer this GitHub library
https://github.com/kakajika/RelativePopupWindow
Here, just need to change the position of the window for example
popup.showOnAnchor(toolbar, VerticalPosition.BELOW, HorizontalPosition.LEFT);

Show popup in recyclerView exactly where the button is, in android

I'm working on a gridLayout with recyclerView in android.I've a an option with each grid Item where I want to show my popup activity which is another class. Please see the image -
There is a menu option with each Item. and my popup activity java name is CustomPop.Class. I use a recyclerView for showing gridViews and its holder method is like
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img_chatroom_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ChatroomPopup.class);
view.getContext().startActivity(intent);
}
});
}
By this I can show the popup activity. But the problem is It's appear in the middle of the activity but I want it to open beside the menu option.
as per my above comment you can use Popup Menu
Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.
try this create menu file
file: poupup_menu.xml
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
than use create popup menu like this
holder.img_chatroom_menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
here is the sample demo links how to create pop-up menu in android
You should have to use popup menu to achieve what you want just as follow :
Create a menu user res > menu > your_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_one"
android:title="Item One" />
<item
android:id="#+id/menu_item_two"
android:title="Item Two" />
</menu>
Put above code into your style.xml
<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textColor">#color/colorBlack</item>
<item name="android:itemBackground">#color/colorWhite</item>
<!--<item name="android:popupBackground">#android:color/white</item>-->
</style>
In your respective RecyclerView Adapter
holder.myAlbumsOptionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
final PopupMenu popup = new PopupMenu(wrapper, v, Gravity.END);
popup.inflate(R.menu.your_menu);
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_one:
//Do operation if menu_item_one
break;
case R.id.menu_item_two:
//Do operation if menu_item_two
break;
}
return false;
}
});
}
});
To know more about PopupMenu

How to match popup Menu on button click to that of createOptionsMenu (or remove shadow)

I have generated a popUp Menu on button click (as the layout is custom layout so couldn't use onCreateOptionsMenu). Everything is working fine except that the menu looks odd. It has got a shadow behind it which doesn't seem to go well with the app. Is there any way in which I can remove the shadow or make the popup menu look like the menu generated using onCreateOptionsMenu.
Below is the image of my popup Menu
Code: (options_menu.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/option1" android:title="#string/option1"
android:orderInCategory="101" android:showAsAction="always|withText" />
<item android:id="#+id/option2" android:title="#string/option2"
android:orderInCategory="102" android:showAsAction="always|withText" />
</menu>
Activity:
popMenuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context wrapper = new ContextThemeWrapper(MenuActivity.this, myPopupMenuTextAppearance);
mPopupMenu = new PopupMenu(wrapper, v);
MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.options_menu, mPopupMenu.getMenu());
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.option1:
startActivity(new Intent(this, XYZ.class));
return true;
case R.id.option2:
someFunction();
return true;
default:
Toast.makeText(this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
}
});
mPopupMenu.show();
}
});

onCreateOptionsMenu for imageview

i am doing a android application for tablet in that
i have a gridView in a view pager
what i want to do is,
i want to add menu to imageview in the gridView like google books
Please see this image for reference
http://i.stack.imgur.com/cf4Or.jpg
i tried placing this in the gridView imageadaptor , no luck
public boolean onOptionsItemSelected(MenuItem item) {
// my code
}
Thanks for your time
You need to use PopupMenu:
http://developer.android.com/reference/android/widget/PopupMenu.html
You need to setup a PopupMenu giving a view as an anchor + specify the menu recourse that will be used here. Of course this should be done inside a OnClickListener for your view.
anchorView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, anchorView);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_one:
// item one clicked
return true;
case R.id.item_two:
// item two clicked
return true;
}
return false;
}
);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
}
});
Define a standard menu resource - here is your popup_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_one"
android:title="Item one"/>
<item
android:id="#+id/item_two"
android:title="Item two"/>
</menu>
Here is a nice tutorial that you can look at:
http://javatechig.com/android/popup-menu-in-android-example
yourImageViewObject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(activity, v);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.spam:
Toast.makeText(activity, "Spam clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.blockuser:
Toast.makeText(activity, " Block user clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.remove:
Toast.makeText(activity, "Remove clicked",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
popup.show();
}
});
menu file main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/spam"
android:title="Spam"/>
<item
android:id="#+id/blockuser"
android:title="Block User"/>
<item
android:id="#+id/remove"
android:title="Remove"/>
</menu>
put above main.xml in menu folder inside res folder
What you need is a PopupMenu. Create a view that can act as the anchor for the menu and add the PopupMenu as follows:
PopupMenu myPopup = new PopupMenu(context, myAnchor);
myAnchor.setOnTouchListener(myPopup.getDragToOpenListener());
You can follow the below steps... I hope this may help you
Step 1 : Create a menu items in menu resource folder
Somewhat like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menuitemone"
android:title="Cut"/>
<item android:id="#+id/menuitemtwo"
android:title="Copy" />
<item android:id="#+id/menuitemthree"
android:title="Paste"/>
</menu>
Step 2: Define an Image view for your individual griditem layout(according to your choice of placement)
Step 3: Define ImageView click listner in your BaseAdaptor class
Step 4: In onclick listner paste this code to show drop down menu
PopupMenu popup = new PopupMenu(MainActivity.this, arg1);
popup.getMenuInflater().inflate(R.menu.menuname, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.i("TEST",item.getTitle());
return true;
}
});
popup.show();
You may need to see if you want a custom design for the same.
Google is your friend.

Android Checkable Menu Item

I have the following menu layout in my Android app:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1"
android:titleCondensed="Options"
android:title="Highlight Options"
android:icon="#android:drawable/ic_menu_preferences" />
<item android:id="#+id/item2"
android:titleCondensed="Persist"
android:title="Persist"
android:icon="#android:drawable/ic_menu_preferences"
android:checkable="true" />
</menu>
My problem is that the second menu item doesn't appear to be "checkable" when I run my app in the Android emulator. There should be a green tick about the item, right? To indicate that its checkable.
Am I doing something wrong?
Layout looks right. But you must check and uncheck menu item in code.
From the documentation:
When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked().
Wrap the items in a group element, like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="all">
<item android:id="#+id/item1"
android:titleCondensed="Options"
android:title="Highlight Options"
android:icon="#android:drawable/ic_menu_preferences">
</item>
<item android:id="#+id/item2"
android:titleCondensed="Persist"
android:title="Persist"
android:icon="#android:drawable/ic_menu_preferences"
android:checkable="true">
</item>
</group>
</menu>
From the Android docs:
The android:checkableBehavior attribute accepts either:
single - Only one item from the group can be checked (radio buttons)
all - All items can be checked (checkboxes)
none - No items are checkable
You can create a checkable menu item by setting the actionViewClass to a checkable widget like android.widget.CheckBox
res/menu/menu_with_checkable_menu_item.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_favorite"
android:checkable="true"
android:title="#string/action_favorite"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />
</menu>
And you can can even style it to be a checkable star if you set actionLayout to a layout with a styled android.widget.CheckBox
res/layout
/action_layout_styled_checkbox.xml
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
res/menu/menu_with_checkable_star_menu_item.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_favorites"
android:checkable="true"
android:title="#string/action_favorites"
app:actionLayout="#layout/action_layout_styled_checkbox"
app:showAsAction="ifRoom|withText" />
</menu>
To set the value
menuItem.setChecked(true/false);
To get the value
menuItem.isChecked()
Cast MenuItem to CheckBox
CheckBox checkBox= (CheckBox) menuItem.getActionView();
I've found that the best solution was to just use the onOptionsItemSelected() method as of my current API (27-28).
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//Copy from here...
int itemId = item.getItemId();
if(item.isChecked())
{
if(R.id.edit_tile_checkbox == itemId) //Individual checkbox logic
{ /*TODO unchecked Action*/}
item.setChecked(false); //Toggles checkbox state.
}
else
{
if(R.id.edit_tile_checkbox == itemId) //Individual checkbox logic
{/*TODO checked Action*/}
item.setChecked(true); //Toggles checkbox state.
}
//...To here in to your onOptionsItemSelected() method, then make sure your variables are all sweet.
return super.onOptionsItemSelected(item);
}
I spent way to long on here for this answer. and for whatever reason, the answers above didn't help (I'm a returning newbie I probably mucked something up I'm sure).
There could be a better way of doing this so helpful criticism is welcomed.
READ THIS
As has been said the "manual checking" is only the tip of the iceberg. It flashes the menu away so fast the users don't see anything happen and it is very counter intuitive, frustrating, and effectively utter crap. The REAL TASK (therefore) is allowing the check box event to be digested by the users mind.
Good news: this can be done and it does work and this is how you do it. #TouchBoarder had it best so I will copy his code. then develop it.
the idea is to detect if the checkbox is clicked, then (and only if that one is picked) slightly suppress the menu removal, add a timer for 500ms then close the menu, this give the "tick" animation of the checkbox time to run and creates the right "feel"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_favorite"
android:checkable="true"
android:title="#string/action_favorite"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />
</menu>
then you make this method as usual, but you make sure you add all this extra bumpf
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the bottom bar and the top bar (weird)
BottomAppBar bottomBar = findViewById(R.id.bottom_app_bar_help);
Menu bottomMenu = bottomBar.getMenu();
getMenuInflater().inflate(R.menu.bottom_nav_menu, bottomMenu);
for (int i = 0; i < bottomMenu.size(); i++) {
bottomMenu.getItem(i).setOnMenuItemClickListener(item -> {
if (item.getItemId()==R.id.action_favorite){
item.setChecked(!item.isChecked());
// Keep the popup menu open
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(frmMain.this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
final Handler handler = new Handler();
handler.postDelayed(() -> bottomMenu.close(), 500);
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
final Handler handler = new Handler();
handler.postDelayed(() -> bottomMenu.close(), 500);
return false;
}
});
return false;
}
else {
return onOptionsItemSelected(item);
}
});
}
return true;
}
the other menu events are here
public boolean onOptionsItemSelected(MenuItem item) {
// Bottom Bar item click
try {
switch (item.getItemId()) {
case R.id.mnuExit:
MenuClick(ClickType.LOGOUT);
return true;
case R.id.mnuList:
MenuClick(ClickType.LIST);
return true;
default:
return super.onOptionsItemSelected(item);
}
} catch (Exception e) {
e.printStackTrace();
}
return super.onOptionsItemSelected(item);
}
Answering because the answers here seem long and convoluted.. I have some exact Kotlin code here
Override your activity at the top and override the function onMenuItemClick, Have a function to handle the button click to open the menu.
Have an array or list which holds the checked value and sets the check when the menu is re-created
Note: This code does not keep the menu open, It only ensures that checked items remain checked.
I noted there are lots of solutions to that on stack overflow, so have a look at them if that's what you desire
class exampleActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
private var checkChecked = arrayListOf(false,false)
//some code
fun clickBTN(v: View){
val popup = PopupMenu(this,v)
popup.setOnMenuItemClickListener(this)
popup.inflate(R.menu.yourmenufilename)
//assuming you have 2 or more menu items
popup.menu[0].isChecked = checkChecked[0]
popup.menu[1].isChecked = checkChecked[1]
popup.show()
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when(item?.itemID){
R.id.item0 -> {
item.isChecked = !item.isChecked
checkChecked[0] = item.isChecked
return true
}
R.id.item1 -> {
item.isChecked = !item.isChecked
checkChecked[1] = item.isChecked
return true
}
}
}
of course in XML you should have your Button and Menu setup. An example menu is here
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item0"
android:title="#string/hi"
android:checkable="true"/>
<item android:id="#+id/item1"
android:title="#string/yo"
android:checkable="true"/>
</menu>
This may be theme dependant but my menu didn't show a checkbox. I found this :
Note: Menu items in the Icon Menu cannot display a checkbox or radio
button. If you choose to make items in the Icon Menu checkable, then
you must personally indicate the state by swapping the icon and/or
text each time the state changes between on and off.
For Adding Menu items Programmatically,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Item1").setActionView(R.layout.action_layout_checkbox).setCheckable(true);
return super.onCreateOptionsMenu(menu);
}
res/layout /action_layout_checkbox.xml
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I have two items in the menu and set to checkable in menu.xml file like below
<item
android:id="#+id/A"
android:title="A"
app:showAsAction="never"
android:checkable="true"/>
<item
android:id="#+id/B"
android:title="B"
app:showAsAction="never"
android:checkable="true"/>
and logic for the menu checkboxes is below.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.A:
//logic goes here
if(item.isChecked())
{
//logic is it is checked
item.setChecked(false);
}
else
{
//logic is it is not checked
item.setChecked(true);
}
return true;
case R.id.B:
//logic for second checkbox goes here
if(item.isChecked())
{
//logic is it is checked
item.setChecked(false);
}
else
{
//logic is it is not checked
item.setChecked(true);
}
return true;
default:
return super.onOptionsItemSelected(item);
}

Categories

Resources