Create a Menu Button instead of using hardware key android - android

I would like to create a menu button in my ActionBar Sherlock that will trigger the menu options I have in a xml file.
I have also created this options_menu but it is only triggered when user clicks on the hardware key (for example in S3, to trigger the menu you should click on left handed side button from the Home button)
I would like to have a button like this on my action bar (the 3 little square button on the right top corner)

The overflow menu is only shown on devices which don't have the hardware keys. It is considered an Action bar Anti-Pattern.
However if you still want to do it, this is how you do it
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}

Related

how to hide the left menu on tap anywhere on android screen xamarin forms?

I am working on xamarin forms. I am using Master detail page to open left menu. And its working fine. Right now menu hide only when user click on menu icon on top. But I want if menu is open, then if user click anywhere on screen, menu should hide.
How I can do this?
I've managed to do it using a custom renderer.
[assembly: ExportRenderer(typeof(BaseMasterDetailPage), typeof(MyMasterDetailPageRenderer))]
namespace Driver.Droid.Renderers
{
public class MyMasterDetailPageRenderer : MasterDetailPageRenderer
{
public override bool OnTouchEvent(Android.Views.MotionEvent e)
{
if (e.Action == Android.Views.MotionEventActions.Up)
new Task(CloseDrawers).Start(TaskScheduler.FromCurrentSynchronizationContext());
return base.OnTouchEvent(e);
}
}
}

Adding menu button to ActionBar Tab

I'm trying to add a menu button to the right side of the a ActionBar tab area
Of course what I really need is a clickable Image/button that is always aligned to the right but I'm not sure how can I do it without major hacking to ActionBar or writing one my self.
Is there a standard way to do it with ActionBar?
Any help will be appreciated.
UPDATE For those who suggested to use the standard way of adding a menu, this is what happens
Thanks
Yoni
You can force the OS to put menu button in action bar but that isn't a good idea. Let the OS decide when to put the menu button on ActionBar.
Anyway I do it like this:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
Log.e("yourapp", ex.toString());
}

menu button doesn't show on nexus 7

so I am facing this problem for long time. I've got Nexus 4 and Nexus 7 both running Android 4.3, and i've got application with targetSdkVersion="11"("I use 11 because any target sdk below 11 doesn't support multitouch for me). And the problem is that 3-dot menu shows on Nexus 4 but doesnt show on Nexus 7. 3 dot menu button on nexus 7 works only if I put targetSdkVersion="8" but then multitouch doesnt work
Nexus 4:
Nexus 7 :
code :
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
screenshots :
nexus 7
nexus 4:
In case you are specifically wondering why the button is not being shown the following rules apply when Android determines if a legacy menu button is needed:
If target API version is less than 11 it is shown on all devices
If target version is 11, 12, or 13 (i.e. tablet-only Honeycomb) Android assumes that your app has been designed for tablets and won't show a legacy button on tablets, but will on phones
If target is 14 or above (ICS and above), Android assumes your app is designed for tablets and phones and so the legacy button isn't shown.
But like the other answers say, you shouldn't be using this menu button. If you don't want an entire ActionBar, another option would to have a three-dot button in your activity which shows a menu using PopupMenu.
You should not be using that menu anymore. From the Menus documentation:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Use an ActionBar.
The correct solution is to use an ActionBar but there may be some hacks to get the overflow menu to appear.
Specifically, there's a hidden window flag FLAG_NEEDS_MENU_KEY you can access via reflection. Here's a code snippet (from this blog):
public static void addLegacyOverflowButton(Window window) {
if (window.peekDecorView() == null) {
throw new RuntimeException("Must call addLegacyOverflowButton() after setContentView()");
}
try {
window.addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
}
catch (NoSuchFieldException e) {
// Ignore since this field won't exist in most versions of Android
}
catch (IllegalAccessException e) {
Log.w(TAG, "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
}
}
I tested this on a couple of Nexus devices and it works. Comments on the blog state that there are devices where it doesn't work. Use with caution. And use an ActionBar, really.
There's a simple way to force a menu option to stay in the menu overflow. If you're creating a menu with XML, you can force this using the "showAsAction" attribute.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_option"
android:showAsAction="never"
android:title="#string/option_name" />
</menu>
If you set "showAsAction" to "never", it will be forced to don't show on the ActionBar, so the option will remain on the menu overflow.
There's more info (like how to vinculate the XML menu file to an Activity) on the official Android documentation webpage: http://developer.android.com/guide/topics/resources/menu-resource.html
I wish this can be helpful!
I wouldnt always recommend using this, since its a hack which breaks the consistency of the phone, but if you want the "3 dots" menu, which is called the overflow menu you need to add this method
//Hack to display overflowMenu on devices with a menu button
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
And in your onCreate() call this method.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity);
getOverflowMenu();
}

setting button in android application

I develop an android application and put it in a android market. users put comments for me and tell me that setting button doesn't work for my application. I don't know what setting button is and how I can enable it for my application.
can any one help me?
I guess what you mean is the menu button. The problem often is that some devices don't have a physical menu button, thus they cannot access the menu of your app.
Try to use a Holo theme for your app's activities and show an ActionBar. By default, the three dots button for your menu will then be shown in the ActionBar on devices that don't have a physical menu button.
For a consistend UI on all devices, you could show the menu button in your ActionBar permanently, whether the device has a physical menu button or not, using this code:
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) { e.printStackTrace(); }
}
Do you use Eclipse?
Check if you have a method onCreateOptionsMenu in your activity class.
Eclipse creates automatically this method and adds a menu in your activity (depending on how you create the activity class file)

How to always show Android settings button on ActionBar?

How can I always show Android settings button on ActionBar? (4.0+)
I would like to show it even if the device has an hardware button for settings, so it would be same with devices with and without hardware buttons.
This is what I'm talking about: http://oi48.tinypic.com/2j104l0.jpg
There is one awful hack which generally have been answered on many questions. Call this from your onCreate():
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
It is highly suggested not to use this hack as sHasPermanentMenuKey field can change anytime. However this has been working for me and others uptil now. See
Android action bar not showing overflow
On a second note, the hardware button is present on the phone for a reason. Just showing the addition overflow menu might confuse your users. Ideally a user would be very much accustomed to using his/her personal phone. So, if the user's phone has a hardware button for overflow menu option, then it need not have the icon on the top of action bar. Having an addition button might confuse users due to difference in behavior in different apps.
Hope that helps.
Edit:
In short, its not recommended as sHasPermanentMenuKey field in the Android code can be changed anytime, which will break your app if not found.

Categories

Resources