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.
Related
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;
}
});
}
}
I have 2 xml one main xml and one menu xml. I want to get the click event from menu xml to main xml activity. is there is any way to solve this problem.
Main Home Page
Menu Page
Now click on the menu page button event i want in my main home activity page.
I done Like this
View otherLayout = LayoutInflater.from(this).inflate(R.layout.menu_layout,null);
Button tstclick = (Button) otherLayout.findViewById(R.id.textclick);
tstclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
System.exit(0);
}
});
The Activity class provides us two methods to inflate/show menu items and work with them or assign some tasks to them. They are:
onCreateOptionsMenu(Menu menu)
onOptionsItemSelected(MenuItem item)
The onCreateOptionsMenu() method is responsible for creating and inflating the menu by help of MenuInflater class.
The onOptionsItemSelected() method is responsible for assigning tasks to each menu item. Each menu item is identified by help of its unique ID.
In order to show and work with menu's in any Activity, both the methods needs to be overridden as shown below:
public class MainActivity extends AppCompatActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
// All your menu items will come here. Each menu item ID will become a separate case in the Switch case
default:
return super.onOptionsItemSelected(menuItem);
}
}
}
As soon as the user clicks or taps on any menu item, the onOptionsItemSelected() method is called by the Android OS and then the ID for that particular menu item will be matched in the method. The group of statements specified in the respective case will then be executed.
For more info visit the following links:
http://developer.android.com/guide/topics/ui/menus.html
http://developer.android.com/reference/android/view/MenuInflater.html
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();
}
}
});
I This is from https://developers.facebook.com/docs/android/scrumptious/authenticate
This is my code (identical from the tutorial) for the options menu part
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
whats happening is this works most of the time but I found a test case I couldn't debug. When you logout, the menu item still shows and you have to click it to make it disappear.(it shouldn't appear at all). This wasn't a huge concern to me at the beginning but I found that when you logged in again after you clicked the menu item to make it disappear, the options menu doesn't appear at all.
I think I found the problem, I leave it up for others. From When and how often onPrepareOptionsMenu() method is called for ActionBar? ,I got that "On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items."
When I logged out, even thought there were no actual menu items, the menu was still open. However when I clicked the menu when it was empty, it messed it up so that when I logged in, show wouldn't work. I don't understand that part yet. I was hoping someone can elaborate what happened during that part
Is there any option to detect a click in overflow menu?
I don't want to detect clicking in particular items.
As it was posted in this other question, you can do the following:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR){
Toast.makeText(this, "OPEN", Toast.LENGTH_SHORT).show();
}
return super.onMenuOpened(featureId, menu);
}
#Override
public void onPanelClosed(int featureId, Menu menu) {
if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR){
Toast.makeText(this, "CLOSE", Toast.LENGTH_SHORT).show();
}
super.onPanelClosed(featureId, menu);
}
If you are inheriting from AppCompat. If not, the right constant to be used is Window.FEATURE_ACTION_BAR
Are you simply trying to detect when the options menu itself is made visible? If so, I believe the "onPrepareOptionsMenu" method on Activity is your best bet.
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
The default implementation updates the system menu items based on the
activity's state. Deriving classes should always call through to the
base class implementation.
http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)