Menu on Fullscreen Activity - android

I have a fullscreen Activity launched by this way:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.preview);
I want to display a menu on the bottom of the screen, by this way:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
with the uiOptions (AndroidManifest) splitActionBarWhenNarrow.
But nothing appears, what is the problem? Thanks

This behavior is because the "hardware" menu button is being phased out in favor of the ActionBar, see this Android Developers blog posting for more information.
Additional information on the ActionBar can be found at:
http://developer.android.com/design/patterns/actionbar.html

You are trying to inflate a layout as a menu
inflater.inflate(R.layout.menu, menu);
Perhaps it should be
inflater.inflate(R.menu.menu, menu);

I had a similar problem when trying to up the TargetSDK on an App that previously had worked fine. I had a full screen app where the user could draw on the entire screen and save from the options menu, but in trying to up the TargetSDK to bring new features to the device I found I lost this feature and all the internet advise was to implement an ActionBar.
After much thought I decided the best option was to implement my own menu from onBackPressed, and show the user a Handy Tip to alert the user of this somewhat odd behaviour when the Activity Was first launched. Obviously the menu needs to have an Exit option or exit the activity when back is pressed again.
It goes against standard Android behaviour, but my only choices were to put an unwanted ActionBar into my full screen activity and not make it full screen any more, or leave the app as a "legacy app" and put no new features in it.

You have problem in following line
inflater.inflate(R.layout.menu, menu);
change to
inflater.inflate(R.menu.menu, menu);
**Also make sure you have not saved
menu.xml
under
layout folder
.save it in menu folder.**
Path will be res->menu->menu.xml

Related

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 overflow menu in bottom bar

I'm trying to get the overflow button from the action bar to show up in the bottom, next to the back/home/other-apps buttons. I was assuming this would be the case if I turn off the title bar, but it doesn't. I tried googling it, but I can't find any solutions.
Who can give me a hint?
For example this app has it:
In your AndroidManifest you have to set the targetSdkVersion to 13 or earlier like this:
<uses-sdk android:targetSdkVersion="13" />
That will force ICS to run your app in compatibility mode and give you the 3 dots in your bottom bar for your overflow menu.
Adding the overflow menu button to the system navigation bar is done to support apps targeting older version of android (2.3 and older) that don't have a hardware Menu button.
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
The article above provides the logic when the system places the overflow button in the nav bar. It doesn't say how to force it to appear though for all devices. but it may help.
Did you try this code for creating option menu?
//Menu options within the app.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

how action bar detect where menu button must show

If application with Action Bar run on tablet - there are menu button in right corner. But on smartphone this button don't show, because device has a hardware menu button, I think.
I need impement similar behavior in my code to show my custom menu button only on tablet, and don't show it on smartphone? It is real?
I don't want use action bar
Thanks
You can check if the device is tablet or smarphone, and inflate the option menu only if the device is tablet.
Suppose you have a method isTablet() that returns true if the device is tablet.
Then you need to override the onCreateOptionMenu() and inflate the menu only if isTablet() returns true.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(isTablet()){
menuInflater.inflate(R.menu.menu_tablet, menu);
return super.onCreateOptionsMenu(menu);
}else{
// do nothing
return true
}
}
How to determine if a device is tabled or phone, you'll need to dig this article: http://developer.android.com/guide/practices/screens_support.html

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.

How to show menu button on Android 3.0?

Currently, I use the phone's menu button to trigger a custom event. On Android 3.0 on the XOOM tablet the menu button does not show. How can I make the button available?
I believe you just don't target Honeycomb. I.e., android:targetSdkVersion="X" where X is less than 11 (and android:minSdkVersion too!)
This will cause your application to be considered a phone application, and the soft menu button will appear.
You should be asking yourself why you want this functionality on a tablet, though.
You can also detect whether the MENU button is present with this:
ViewConfiguration.get(context).hasPermanentMenuKey()
And if not, you can show a custom menu button in your UI.
In your res folder make sure you have a "menu" folder.. inside that folder you will need menu.xml file..... the xml file should contain information that looks similar to this:
<menu xmlns:android="https://schemas.android.com/apk/res/android" xmlns:android1="http://schemas.android.com/apk/res/android">
You can change out the item section to reflect your own menu.
Make sure you have the xml file saved...
Then go into your main code and do the following:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
that's how I do it.

Categories

Resources