Show Custom Menu On Menu Button Click - android

I have created an Activity that will behave like a popup menu is actually just a list of Menu Items. I then set the theme of the activity on my manifest to "Dialog" to get the popup effect. Now I need this activity to show when the Menu button is pressed on the device. I tried using onCreateOptionsMenu, then passing my Activity XML to the Inflater like so:
public boolean onCreateOptionsMenu(Menu menu){
new MenuInflater(this).inflate(R.layout.popup_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
"popup_menu" is the XML for my Activity. But this didnt work. Any other suggestions??

you should start your activity there. I can tell you more if I see the code for your activity

Related

MaterialSearchView doesn't get focus on startup - Android

I am using MaterialSearchView (https://github.com/MiguelCatalan/MaterialSearchView), a library to implement SearchView in a Material Design Approach.
It puts a searchview in the toolbar. However don't know how to focus on the search, so that when the actviity starts, the search gets the focus right away and the virtual keyboard appears. It only focuses if i click on the search icon.
MaterialSearchView searchView;
searchView = MaterialSearchView)getActivity().findViewById(R.id.search_view);
searchView.requestFocus();
doesn't work.
I tried it from the fragment and from the activity.
Thanks
I would suggest you to use my implementation of MaterialSearchView, it's possible to open it up without clicking on the Toolbar/Actionbar.
For example, in your Activity you could do the following:
// Activity:
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
Then, anywhere in your code, you could open the view by calling:
searchView.openSearch();
And close it by calling:
searchView.closeSearch();
So, you could put the openSearch() inside your onResume() for example. That way, you'll always have the search view open when your Activity starts. You could also call that method from any Button on your Activity.
P.S.: It also have support for search suggestions and search history.
Bonus: Have a gif (It's shown in portuguese, but it also supports english, so don't worry).
For more information on how to set it up, and to see the use cases, you can check the docs.
Create a menu reference object:
private Menu menu;
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
//rest of your code here ...
}
and then onResume() of your Activity you can programmatically simulate item click like this:
onOptionsItemSelected(menu.findItem(R.id.searhView));

Android: Set menu visibility within a fragment

I have an android app that has a side menu and the main body of content is inside a fragment. So far there are 3 fragments (Home, Settings, Help). What I am trying to do is have the menu in the top right only appear in the home fragments.
Using
this.setHasOptionsMenu(false);
doesn't do anything so I am obviously using that wrong. Any ideas on how I can accomplish this?
Use:
menu.findItem(R.id.MENU_ITEM).setVisible(false);
For all of your menu items in the method onPrepareOptionsMenu(...). You should end up with something similar to this:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.MENU_ITEM_ONE).setVisible(false);
menu.findItem(R.id.MENU_ITEM_TWO).setVisible(false);
menu.findItem(R.id.MENU_ITEM_THREE).setVisible(false);
}

Toolbar in DialogFragment

In our app, we have a few fragments that can be shown either as fullscreen fragments or as dialog fragments. These fragments inherit from DialogFragment and we make sure to instantiate them correctly depending the mode the app is executed in (either fullscreen or dialog).
We thought about adding some extra functionality to some of these dialog fragments after the latest changes in the Toolbar widget were introduced in the support library with Lollipop. The idea is to have the type of options menu we would normally have in an ordinary fragment (i.e. the options menu inflated after onCreateOptionsMenu is executed) present in our subclasses of DialogFragment ONLY when these are visualized as dialogs. In short: when the fragments are shown in fullscreen mode we inflate a traditional options menu, and when the fragments are shown as dialogs we would like to have the same options menu inflated but using the new toolbar widget in standalone mode.
I followed the steps from http://android-developers.blogspot.dk/2014/10/appcompat-v21-material-design-for-pre.html and I managed to "activate" the toolbar, but it seems the menu is not inflated - see attached screenshots (picture one fragment in fullscreen mode, and picture two in dialog mode).
Is it even possible to inflate an options menu with the new toolbar in a DialogFragment?
Is it even possible to inflate an options menu with the new toolbar in
a DialogFragment?
first of all your design is ok and toolbar is supposed to be used every where lets have a look at this from Chris Banes google engineer link:
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
and also android developer toolbar standalone sample:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
Yes, DialogFragment has setHasOptions() function. Define the toolbar in the layout of your dialog and use it as if it is in an activity. A toolbar doesnt mind being in an activity or a fragment or a dialog fragment.......
Be sure that you use
setHasOptionsMenu(true) in onActivityCreated method....
Then, as usual override
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.my_menu, menu);
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

What does onPrepareOptionsMenu do?

I want to make Option Menu for Android, I have visit this site. In their script, I found onPrepareOptionsMenu, I try to compile and run using Android 2.3.3 compiler with and without onPrepareOptionsMenu, both works, but I didn't see any difference.
public boolean onCreateOptionsMenu(Menu menu){
//code here
}
public boolean onOptionsItemSelected(MenuItem item){
//code here
}
public boolean onPrepareOptionsMenu(Menu menu){
//code here
}
What is actually onPrepareOptionsMenu method do? Is that method important? Could I just delete the method?
Addition
Oh, I also hear about Action Bar in Android 3.0, it says that Action Bar is the alternative way for make Option Menu, and it using onPrepareOptionsMenu. Is that right?
Thank you...
Take a look in the API:
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.
If you want to alter the menu before it's shown to the user, you can put code to do that into onPrepareOptionsMenu. I've used that dynamically to disable some menu options in some circumstances.
As an example of when one might want to disable a menu option, I had an app where there was a way of specifying a destination. One of my menu options was to calculate a route to the destination. However, if a destination wasn't specified, that option didn't apply, so I used onPrepareOptionsMenu to disable that menu option when it wasn't applicable.
From Android 3.0 and beyond, there's the ActionBar, which is a menu bar. The most important items go into the ActionBar itself, but then there's an overflow for when there's not enough room on the action bar. One can specify that menu items should always be in the overflow menu and never on the action bar itself. On some devices, the action bar overflow corresponds to the permanent menu button on the device, whereas on other devices which don't have a menu button the overflow menu is seen on the right hand side of the action bar as three vertical dots.
onCreateOptionsMenu is called once, when your activity is first created. If it returns false, no option menu is shown and onPrepareOptionsMenu is never called.
If onCreateOptionsMenu returns true, onPrepareOptionsMenu is also called before the activity is displayed, and also every time the options menu is invalidated. Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.
If your menu does not change, use onCreateOptionsMenu.
example
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
if(!URLUtil.isValidUrl(news.geturl())){
menu.findItem(R.id.share).setVisible(false);
}
}

Android sdk. inflate menu(xml file) on button press

I understand that after Android 3 menu buttons are not supported. So, I simply want to use a buton on the screen to inflate the menu. I currently use an overridden
public boolean oncreateoptionsmenu(Menu menu) { MenuInflater inflater=getMenuInflater();
inflater.inflate( R.menu.menu, menu); return true}.
This script runs on menu button press, but I also want it to run on my button press. How can this be done? What data is sent to the Menu parameter ( the menu to inflate to)?
Thanks,
On android 3+ the menu will be added to the actionbar automatically when the activity starts. There is no need to change anything.

Categories

Resources