Android: how to open menu where user clicks - android

Is it possible to open a menu in the exact point where the user has clicked?
I have a custom listview and I'd like to show a menu like the right click on Windows in the point where the user clicks on the listview item.

Absolutely. Take a look at PopupMenu, which lets you do exactly that. Here's an example of how to use it:
listItemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Create an instance of the PopupMenu
PopupMenu menu = new PopupMenu(MainActivity.this, listItemView);
// Inflate the menu using a menu layout file
menu.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Do something here if correct MenuItem selected
return true;
}
});
}
}

Related

Anchoring a PopupMenu to Navigationview menu items

Since this question and its answer didn't show up anywhere, thought I might as well contribute a little for once by sharing the way I found.
So I had this issue with anchoring a PopupMenu object to a NavigationView menu since I couldn't get any items out of it in the form of a view:
//onCreate:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
drawerMenu = navigationView.getMenu();
//onNavigationItemSelected(MenuItem menuItem):
switch (menuItem.getItemId()) {
case R.id.menubutton_submenuButton:
//the following line is merely how I'd imagine it should be
PopupMenu popupMenu = new PopupMenu(this, drawerMenu.findItem(R.id.menubutton_submenuButton));
popupMenu.getMenuInflater().inflate(R.menu.sub_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//handle the inflated menu's buttons here
return true;
}
});
popupMenu.show();
break;
}
So then, the problem is basically where
drawerMenu.findItem(R.id.menubutton_submenuButton));
isn't a View of any sort, which leaves the PopupMenu with nothing to anchor to.
So I figured you can just create an empty view inside the menu item as follows:
create a layout xml file including only the following:
<?xml version="1.0" encoding="utf-8"?>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
lets call it "view_empty".
Now the menu item should have that empty view layout included inside of it by adding the following line to its item: app:actionLayout="#layout/drawer_empty"
My item looks as follows:
<item
android:id="#+id/menubutton_submenuButton"
android:title="example menu item"
app:actionLayout="#layout/view_empty"/>
Now then, all that's left is just using it:
//onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menubutton_submenuButton:
MenuItem item = drawerMenu.findItem(R.id.menubutton_submenuButton);
PopupMenu popupMenu = new PopupMenu(this, MenuItemCompat.getActionView(item));
popupMenu.getMenuInflater().inflate(R.menu.sub_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//handle the inflated menu's buttons here
return true;
}
});
popupMenu.show();
break;
What'd happen now is that the PopupMenu would anchor itself to an invisible view inside the menuItem, thus making it look like the container menuItem is the anchor point.

Toolbar menu item programmatic click

I have this code
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openSearch();
return true;
}
});
into onCreate( ) of my Activity.
OpenSearch function call opens up google now like search view. This only happens when the user clicks on the search action item in the toolbar. In my case I want the search view to open up automatically when the activity starts. How can this menu item click be done programmatically.
I can't call openSearch directly because it needs the menu to be created.
Is there there any callbacks informing that action menus have been created ?
You can try something like this :
tollbarLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tollbarLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tollbar.performclick();
}
}
});

android: Launch the navigation bar dropdown menu with a button

I'm trying to write code so that when a button is pressed, the dropdown menu in the navigation bar comes up. I tried making something similar to the following prefab function (which I understand to be the one responsible for opening the menu)
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_class, menu);
return true;
}
Problem is that I don't know where they get the menu variable from. What should I put in it's place to complete the following function?
public void launchMenu(View view) {
getMenuInflater().inflate(R.menu.main_class, (Menu) /* menu variable */);
}
Create this method in MainActivity which contains your navigation drawerLayout.
public void open()
{
mDrawerLayout.openDrawer(Gravity.LEFT);
}
Check this SO question for more info.
I think you are looking for something like this...
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}

Menu button works just ONE time

I want to show a Menu when click a specific image :
popup_but = (ImageView) findViewById(R.id.imageView2);
popup_but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showmen();
}
});
And the menu :
public void showmen() {
PopupMenu popup = new PopupMenu(First.this, popup_but);
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.men1:
//do something
return true;
case R.id.men5:
finish();
return true;
}
return true;
}
});
popup.show();
}
It. works. now i want to do the same when click the hardware menu button. so i use this code :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
showmen();
}
The problem is here : when menu button clicked it just show menu for the FIRST time
Take a look in the onPrepareOptionsMenu:
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu()
each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, the options menu is considered to always be
open when menu items are presented in the action bar. When an event
occurs and you want to perform a menu update, you must call
invalidateOptionsMenu() to request that the system call
onPrepareOptionsMenu().
onCreateOptionsMenu() is called every time the hardware menu button is clicked, and if it returns true each time. What I do is, style the menu items using XML and then inflate the XML inside onCreateOptionsMenu() . Every time I click the hardware menu, the menu is inflated from XML. onPrepareOptionsMenu() is only for when you want to change the menu items during the lifecycle of the app. The problem, I think is in your implementation of showmen(). I think the PopupMenu.OnMenuItemClickListener is called only the first time, not every time.

How to add a drop down menu on Actionbar without the preselected item occupying space

I would like to add on the android Actionbar a drop down menu without a preselected option. More precisely I would like the arrow to be next to the application's icon on actionbar. So far the code is:
protected void onCreate(Bundle savedInstanceState) {
.
.
.
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.logo_options,
android.R.layout.simple_spinner_dropdown_item);
actionBar.setListNavigationCallbacks(spinnerAdapter, null);
}
How can I get rid of the text on the actionbar?
You can create a popup menu which drops down whenever you click on action bar button:
View menuItemView = findViewById(R.id.apps);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.windowitem1)
//Do whatever you want to
if (item.getItemId() == R.id.windowitem2)
//Do whatever you want to
if (item.getItemId() == R.id.windowitem3)
//Do whatever you want to
return false;
}
});
popupMenu.show();
ActionBar text can be removed by android:showAsAction="" property,you can set it accordingly.....

Categories

Resources