In my application i want to add toggle button right side of the Application-name (ie)Right side of BluetoothTextMessaging
Please help me thanks...
May this help you:
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.addSubMenu(0, 1, 1, " Button ").setIcon(R.drawable.file_icon)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
return true;
}
And you can access that button using following code & write logic what to perform on click of that button inside the following code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: {
// Your Logic
break;
}
return super.onOptionsItemSelected(item);
}
I believe that what you're searching for is called an options menu. On Android 3.0+ it is supported. This is how Google's own tutorial describes it:
If you've developed your application for Android 2.3.x (API level 10)
or lower, the contents of your options menu appear at the bottom of
the screen when the user presses the Menu button, as shown in figure
When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six
items, Android places the sixth item and the rest into the overflow
menu, which the user can open by selecting More.
If you've developed your application for Android 3.0 (API level 11)
and higher, items from the options menu are available in the action
bar. By default, the system places all items in the action overflow,
which the user can reveal with the action overflow icon on the right
side of the action bar (or by pressing the device Menu button, if
available). To enable quick access to important actions, you can
promote a few items to appear in the action bar by adding
android:showAsAction="ifRoom" to the corresponding elements
Follow this link: (Menu tutorial) to get to the tutorial.
I can provide code snippets, however the ones on the website are much better :-)
Related
I have to problems:
• When the user taps on the three dots (the overflow menu), I don't want the app to display a menu. Instead I want it to do an action instantly.
• The action should be a change to the AppTheme. So when the user taps on the overflow menu, the app theme changes to dark, if the user taps it again to light, etc.
Since it's not a good way or as #David Medenjak said, it might be a different design or bad design (something like out of the rules).
By the way, you can use this for handling that OverflowMenu:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (disableMenu())
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
return true;
}
And then use the following answer:
https://stackoverflow.com/a/18301723/4409113
Finally, use the following link for checking what the current theme is and at the end, set your theme.
Hope that helps.
I'm targeting Android >=4 and I have an issue with the ActionBar.
I need to dynamically update the action bar and all the code seems to work fine on a Nexus 7 and Nexus 4. The problem I have is with an Xperia S (Android 4.1.2). The Nexus devices update the ActionBar immediately. The Xperia only updates when the hardware menu button is pressed.
I call invalidateOptionsMenu() which in turn successfully calls the onPrepareOptionsMenu(Menu menu) on all devices. The difference is that the Xperia is simply not updating the display. As soon as I click the hardware menu button, up pops the menu items and buttons.
I've read quite a few posts and tried numerous methods - I simply can't get the Xperia to behave properly.
I've messed around with this and the best I can come up with is to recreate the activity which gives and unacceptable user experience.
Everything works fine on devices with soft menu buttons. Until I can come up with something better I've decided to force all menu items on to the action bar drop down menu for devices with a hardware menu button.
I detect the hardware menu button in the onCreate of the Activity;
this.hardwareMenuButton = ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
Then I have something like this;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("Refresh");
if(this.hardwareButton) {
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_NEVER);
} else {
menuItem.setIcon(R.drawable.ic_action_refresh);
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onPrepareOptionsMenu(menu);
}
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);
}
}
I have an application which runs full-screen and relies on the menu button. What I didn't realise is that devices like the Galaxy Tab use an actionbar which no longer has a menu button. My app currently loads a fragment displaying a settings menu when one touches the menu button:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
FragmentManager lFM = getSupportFragmentManager();
SettingsDialogFragment lSettingsDialog = new SettingsDialogFragment();
lSettingsDialog.show(lFM, "Settings");
return false;
}
I understand it is possible to add a custom icon to the action bar which when pressed could result in the same behaviour as a menu button. What I am unsure about is how to implement this.
How do I know that a device doesn't have a menu button and I need to add an icon to the action bar? It can't be as easy as checking the SDK version as apparently the actionbar was introduced in honeycomb, but my Galaxy Note runs ice cream sandwich and doesn't have an action bar (it still has a menu button). I don't want to give up any real-estate so adding buttons or menu options to my main layout isn't an option.
I just read on google developer that:
Navigation Bar New for phones in Android 4.0, the navigation bar is
present only on devices that don't have the traditional hardware keys.
It houses the device navigation controls Back, Home, and Recents, and
also displays a menu for apps written for Android 2.3 or earlier.
So I tried setting the target version on my app to 8. Instead of a menu I get a button allowing me to change the screen size of my app - but no menu button.
I have an application which runs full-screen and relies on the menu button.
That has been a bad idea for two years.
What I didn't realise is that devices like the Galaxy Tab use an actionbar which no longer has a menu button.
Such devices have been around for two years.
My app currently loads a fragment displaying a settings menu when one touches the menu button
That was never an appropriate design move. Please allow the MENU button, where it exists, to behave normally, displaying an options menu on Android 1.x/2.x and triggering the action bar overflow on Android 3.0+.
How do I know that a device doesn't have a menu button
http://developer.android.com/reference/android/view/ViewConfiguration.html#hasPermanentMenuKey()
in my app the android default menu button is not visible? why? Any help?
Refer this:
On Android Honeycomb how do I add option menu to the home bar at the bottom of the screen?
And this:
onCreate Options menu is not showing in android 3.1
You can simple edit target SDK in manifest file to force use SDK of mobile rather than tablet like this:
android:targetSdkVersion="10"
Not only should your apps stop relying on the hardware Menu button, but you should stop thinking about your activities using a “menu button” at all.Your activities should provide buttons for important user actions directly in the action bar (or elsewhere on screen). Those that can’t fit in the action bar end up in the action overflow.
In order to provide the most intuitive and consistent user experience in your apps, you should migrate your designs away from using the Menu button and toward using the action bar.
Ice Cream Sandwich rolls out to more devices, it’s important that you begin to migrate your designs to the action bar in order to promote a consistent Android user experience.See this link
Did you hook up a xml menu file to your activity ?
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.xmlfilename, (android.view.Menu) menu);
return true;
}