Android Check if the device has a navigation bar Navigationbar

Source: Internet
Author: User

Respect for the original, respect for the author, reproduced please indicate the source:

http://blog.csdn.net/lnb333666/article/details/41821149

There is no reliable way to check if there is a navigation bar on the device. You can use Keycharactermap.devicehaskey to check if there are some physical keys on the device, such as menu keys, return keys, and home keys. Then we can determine whether there is a navigationbar by physical key or not (usually the physical keys, navigationbar coexist on the phone).

[Java]View Plaincopyprint?
  1. Public static int getnavigationbarheight (activity activity) {
  2. Resources resources = Activity.getresources ();
  3. int resourceId = Resources.getidentifier ("Navigation_bar_height",
  4. "Dimen", "Android");
  5. //Get the height of Navigationbar
  6. int height = resources.getdimensionpixelsize (resourceId);
  7. return height;
  8. }


The above code, in the vast majority of cases can be obtained to the height of the navigationbar. So some people want to use this height to judge whether there is navigationbar is not possible. Of course, the 4.0 version below is needless to say. Confirm a question, Navigationbar is 4.0 above only?

Because the device has a physical key, you can still have a navigation bar. Any device that runs a custom ROM will set an option to disable the physical key and add a navigation bar. Look at the API:

Viewconfiguration.get (context context). Haspermanentmenukey () has a description: report if the device has a permanent menu key avail Able to the user (report if the device has a permanent menu that is primarily available to users).


Description of the Android.view.KeyCharacterMap.deviceHasKey (int keycode): Queries The framework about whether any physical keys exist on The any keyboard attached to the device that is capable of producing the given key code (the query framework exists for any physical keyboard that is connected to the production of any keyboard to give key codes 's ability. ).

So the solution is:

[Java]View Plaincopyprint?
    1. @SuppressLint ("Newapi")
    2. public Static Boolean Checkdevicehasnavigationbar (Context activity) {
    3. determine if there is a navigation bar by judging whether the device has a return key, a menu key (not a virtual key, or a button outside the phone screen)
    4. Boolean hasmenukey = Viewconfiguration.get (activity)
    5. . Haspermanentmenukey ();
    6. Boolean hasbackkey = Keycharactermap
    7. . Devicehaskey (Keyevent.keycode_back);
    8. if (!hasmenukey &&!hasbackkey) {
    9. //Do whatever you need to do, this device has a navigation bar
    10. return true;
    11. }
    12. return false;
    13. }

Android Check if the device has a navigation bar Navigationbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.