I am using a popup menu in my android application whenever the button is clicked the popup menu appears blank I have inflated a menu layout file with the popup menu. The actions are triggered but the popup menu appears to be blank. here's the screenshot of popup menu.
My adapter code:
holder.more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
popupMenu.inflate(R.menu.expense_history_menu);
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Edit:
Toast.makeText(context, "Edit clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.Delete:
spent_by.remove(position);
category.remove(position);
desc.remove(position);
date.remove(position);
share_by.remove(position);
notifyItemRemoved(position);
helper.delete_spent_history(s_id.get(position));
tot_amt = helper.get_trip_total_amt(t_id);
helper.update_total_amt(t_id, tot_amt);
update_due_amt();
return true;
default:
return false;
}
}
});
}
});
My expense_history_menu layout file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/Edit"
android:title="Edit"/>
<item
android:id="#+id/Delete"
android:title="Delete"/>
</menu>
Help me to solve this problem.
Thanks in advance!
Paste this in style.xml
and make changes as required.
<style name="PopupMenu" parent="AppTheme">
<item name="android:popupBackground">#color/colorPrimary</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
Don't forget to add style="#style/PopupMenu" in expense_history_menu
Related
I have used bottom app bar and the problem is when i used a custom layout for my menus using app:actionLayout in menu, the click is not working for these menus i.e setOnMenuItemClickListener for bottomappbar not working.?
It is working fine when actionLayout not used.
BottomAppBar bottomAppBar;
bottomAppBar = findViewById(R.id.bar);
bottomAppBar.replaceMenu(R.menu.announcement_menu);
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_all:
Log.d("Announcement", "All clicked");
break;
case R.id.action_rec:
Log.d("Announcement", "Received clicked");
break;
case R.id.action_sen:
Log.d("Announcement", "Sent clicked");
break;
}
return false;
}
});
And the menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_all"
app:actionLayout="#layout/custom_menu_row_all"
android:title="All"
app:showAsAction="always" />
<item
android:id="#+id/action_rec"
app:actionLayout="#layout/custom_menu_row_received"
android:title="Received"
app:showAsAction="always" />
<item
android:id="#+id/action_sen"
app:actionLayout="#layout/custom_menu_row_sent"
android:title="Sent"
app:showAsAction="always" />
</menu>
Got working with following:
View v = bottomAppBar.getMenu().findItem(R.id.action_rec).getActionView().findViewById(R.id.tvTitle);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Announcement", "Received clicked");
}
});
I want to a pop up like this is facebook
Hello Guys,
Above is the image where you can see a popup comes over a button. I tried achieving this using AleartDialog but it opens in center. I want it below that button only.
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_show_options, null);
new AlertDialog.Builder(this)
.setView(view)
.create().show();
Any help would be appreciated. Thanks
Use Popup menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/unfriend"
android:icon="#drawable/ic_mail"
android:title="Unfriend" />
<item android:id="#+id/edit_friend_list"
android:icon="#drawable/ic_upload"
android:title="Edit FriendList"
android:showAsAction="ifRoom" />
</menu>
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_example, popup.getMenu());
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.unfriend:
//
return true;
case R.id.edit_friend_list:
return true;
default:
return false;
}
}
Hope it will help.
for more detail please visit below link.
https://www.tutlane.com/tutorial/android/android-popup-menu-with-examples
https://www.javatpoint.com/android-popup-menu-example
http://www.coderzheaven.com/2013/04/07/create-simple-popup-menu-android/
Use Pop Up menu
open menu on your button click
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mail"
android:icon="#drawable/ic_mail"
android:title="#string/mail" />
<item android:id="#+id/upload"
android:icon="#drawable/ic_upload"
android:title="#string/upload"
android:showAsAction="ifRoom" />
<item android:id="#+id/share"
android:icon="#drawable/ic_share"
android:title="#string/share" />
</menu>
Java Code:
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
I want to place a dropdown list on the right. Can I do this in a standard spinner?
I think Image represented is PopUp menu, To achieve the same see the code below,
To define popup menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (popup_menu.xml) file to build the menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/first_item"
android:title="first" />
<item android:id="#+id/second_item"
android:title="second" />
<item android:id="#+id/third_item"
android:title="third" />
</menu>
In your Activity,
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
Now Override onMenuItemClick to provide functions to each item.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_item:
// do your code
return true;
case R.id.second_item:
// do your code
return true;
case R.id.third_item:
// do your code
return true;
default:
return false;
}
I need to show the submenu below the bar, not on top of the bar itself.
Copying my actionbar xml below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="#+id/action_pages"
android:orderInCategory="1"
android:showAsAction="withText|always"
android:icon="#drawable/ic_action_pages"
android:title="">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
</menu>
In the activity (App also has a navigation drawer)
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Simply.
<style name="AppTheme" parent="AppBaseTheme">
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
</style>
Preamble
As usual, I faced a strange problem while developing an Android app, tried to find a solution and landed to this question. As it was in many cases before, there is no an answer. So I was compelled to solve the problem from scratch and now posting the answer with my workaround.
Input
I have an Android app with action bar and some menu items, which have to be extended with dropdown submenu. First attempt was to implement it as suggested by Android documentation. So I added new menu item menu_sort into existing action bar menu and sub-menu container into it:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/id1" android:icon="#drawable/ic_1"
android:title="#string/id1" android:showAsAction="withText|always"/>
...
<item
android:id="#+id/menu_sort"
android:icon="#drawable/ic_menu_sort_selector"
android:title="▾"
android:titleCondensed="▾"
android:showAsAction="withText|always">
<menu>
<item
android:id="#+id/menu_sort_by_name"
android:showAsAction="never"
android:checkable="true"
android:checked="true"
android:title="#string/sort_by_name"/>
<item
android:id="#+id/menu_sort_by_priority"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_priority"/>
<item
android:id="#+id/menu_sort_by_memory"
android:showAsAction="never"
android:checkable="true"
android:checked="false"
android:title="#string/sort_by_memory"/>
</menu>
</item>
</menu>
Result
The effect was exactly as described in the question: the submenu is displayed on top of the action bar. Here is the screenshot taken on Android 5.1.1:
I played with many options and code snippets - nothing helped. Finally I came to the following
Solution
First, move all the submenu into a separate menu layout, say, menu/sorting.xml, and remove it from menu_sort item of the main menu (shown above).
Second, modify or create onPrepareOptionsMenu event handler with the following code:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// as solution utilizes PopupMenu,
// take care about older Android versions if necessry
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
// here goes most crazy part: we use menu id
// to retrieve corresponding view, automatically created by OS;
// imho, this is a hack, and menu item should have getView() method or similar;
View menuItemView = findViewById(R.id.menu_sort);
// by the way, menuItemView could probably be null under some circumstances
// create a popup anchored to the view (menu item)
final PopupMenu popupMenu = new PopupMenu(this, menuItemView);
// API 14
// popupMenu.inflate(R.menu.sorting);
// API 11 (HONEYCOMB)
popupMenu.getMenuInflater().inflate(R.menu.sorting, popupMenu.getMenu());
// process popup clicks as appropriate
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
// ... place some code
}
return true;
}
});
// bind the popup to the item menu
menu.findItem(R.id.menu_sort).setOnMenuItemClickListener(new OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
Here is the result:
Now the dropdown is displayed as expected from very beginning.
#Stan's solution doesn't work for me, so here's my way to implement sub-menu on top of ActionBar (but below the main-menu of course):
I've created 2 xml files: menu_main.xml and menu_more.xml located in res/menu directory
The first one 'menu_main.xml' contain the menu:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- our addMenu doesn't have sub-items-->
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_note_add_white_24dp"
android:title="#string/action_add"
app:showAsAction="ifRoom"/>
<!-- our moreMenu which show drop-down menu when clicked-->
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_vert_white_24dp"
android:title="#string/action_more" <!--in text: "more"-->
app:showAsAction="always"/>
</menu>
And the second one 'menu_more.xml' contain the drop-down menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- This menu will be hidden by default-->
<!-- But will be visible when moreMenu with '#+id/action_more' is clicked-->
<item
android:id="#+id/action_settings"
app:showAsAction="ifRoom|withText"
android:title="#string/action_settings" <!-- In text: "Settings"-->
android:visible="true"/>
</menu>
Here is what previous menus look like:
result-after-add-2-xmls (i have not enough 10 reputation to display image)
In the activity, i've overridden this method:
public boolean onPrepareOptionsMenu(Menu menu)
In the previous method, i get reference to the main menuItem (in this case is menu with #+id/action_more located in menu_main.xml file), then set setOnMenuItemClickListener on it and finally, declare and set up a PopupMenu instance to manage and display sub-menu items:
// show popup menu when menuMore clicked
menu.findItem(R.id.action_more).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
// get reference to menuMore item
View menuMore = findViewById(item.getItemId());
// create a popup anchored to the view (menuMore)
// notes: if declare and set up PopupMenu Outside of this onMenuItemClick()
// then it'll not work!
// Because: the view you put into PopupMenu() could be null
final PopupMenu popupMenu = new PopupMenu(getApplicationContext(), menuMore);
// inflate 'menu_more.xml' layout file
// which contain all sub-items of menu
popupMenu.getMenuInflater().inflate(R.menu.menu_more, popupMenu.getMenu());
// process popup clicks on sub-items
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "showing SettingsActivity..",
Toast.LENGTH_SHORT).show();
break;
// more items go here
}
return true;
}
});
popupMenu.show();
return true;
}
});
return super.onPrepareOptionsMenu(menu);
And here is final result:
final-look-drop-down-menu
I made an Navigation-Drawer on the left and a menu on the right but my menu on the right (3 dots menu) is over the blue line...
I had the same problem with my Title text-view but i simply made his background-color transparent to fix it. Sadly i haven't been able to do the same with my menu.
Is there a way to make my menu background transparent or a work around to make my blue line go over it?
Here is how i made my menu:
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_view, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Menu options
switch (item.getItemId()) {
case R.id.map_view:
Toast.makeText(getApplicationContext(), "Map view pressed",
Toast.LENGTH_LONG).show();
//startActivity(GoogleMapsActivity.class);
return true;
case R.id.infrastucture_only:
Toast.makeText(getApplicationContext(),
"Infrastucture only pressed", Toast.LENGTH_LONG).show();
return true;
case R.id.infrastucture_map:
Toast.makeText(getApplicationContext(),
"Infrastucture + map pressed", Toast.LENGTH_LONG).show();
return true;
case R.id.camera_only:
Toast.makeText(getApplicationContext(), "Camera only pressed",
Toast.LENGTH_LONG).show();
return true;
case R.id.camera_infrastucture:
Toast.makeText(getApplicationContext(),
"Infrastucture + Camera pressed", Toast.LENGTH_LONG).show();
//startActivity(MetaioActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/dots_menu"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="#string/dots_menu">
<menu>
<item
android:id="#+id/map_view"
android:icon="#drawable/ic_action_map"
android:showAsAction="never"
android:title="#string/map"/>
<item
android:id="#+id/infrastucture_only"
android:showAsAction="never"
android:title="#string/infrastucture_only"/>
<item
android:id="#+id/infrastucture_map"
android:showAsAction="never"
android:title="#string/infrastucture_map"/>
<item
android:id="#+id/camera_only"
android:showAsAction="never"
android:title="#string/camera_only"/>
<item
android:id="#+id/camera_infrastucture"
android:showAsAction="never"
android:enabled="false"
android:title="#string/camera_infrastucture"/>
</menu>
</item>
</menu>
Thank you :)
I fixed the problem. I forgot to go to my style.xml file and add the transparency to my custom style like this:
<style name="MyActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
</style>
Now i don't know why when i open the menu it's not transparent
http://i821.photobucket.com/albums/zz140/miguel_melo_43/openWindow_zpse164f5b7.png
Is there a better way to make the menu so i can customize like i did with my navigation drawer?
http://i821.photobucket.com/albums/zz140/miguel_melo_43/NavDrawer_zpscaf04f64.png