0

I have been struggling to find a reliable solution to solve an issue from the past two weeks. In our android app, we make use of a back ground image. There are some borders in this image in which we have to place different views. The application needs to be supported from 4.0 to current latest version. The issues I am facing are listed below.

The app when installed on devices with navigation bar looks perfect, but when run on devices without navigation bar the alignment is disturbed.

What did I try? I tried checking if the OS version is 18 (4.3) and according to that created two layouts for each activity and installed. The code is here

if (DEVICE_API_VERSION > 17) {
    setContentView(R.layout.layoutwithnavigationbar);
            } else {
    setContentView(R.layout.layoutnonavigationbar);
            }

But there are devices like samsung galaxy note II, which are upgradable to 4.4.2 and have no navigation bar.

Then I tried out the following proceedure.

    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    boolean hasMenuKey = ViewConfiguration.get(mContext).hasPermanentMenuKey();
    System.out.println("has back key . . . . "+hasBackKey);
    System.out.println("has home key . . . . "+hasHomeKey);
    System.out.println("has menu key . . . . " + hasMenuKey);
            if(hasHomeKey || hasBackKey) {
                setContentView(R.layout.layoutnonavigationbar);
            } else {
                setContentView(R.layout.layoutwithnavigationbar);
            }

But again I had no luck. In sony xperia sl which is 4.1.2 version of android, xhdpi, no navigation bar picks the layout perfectly, but in sony xperia m2, returns hasBackKey as true even if it is having a navigation bar and has no hardware menu buttons, and picks up layout with no navigation bar and layout looks ugly. Even tried few more things like resource id of navigation bar but in htc one s which has hardware buttons and no navigation bar, has a resource id for navigation bar. so it didn't work. I got the resource id as shown below.

int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");

Did anyone have this problem? Am I doing any thing silly or missing any thing?

What I am looking for?

1) My app should work from 4.0 to latest version.

2) If any device got navigation bar it can either be made completely transparent and use screen below that too, so that UI is not affected or any other better solution is also fine.

3) Want to know if there is any standard way to know if the device has a navigation bar or uses hardware buttons?

4) How can I make the navigation bar transparent in devices prior to Kitkat.

Note : App only is in landscape mode. Even tried transparent Navigation bar themes but they are only supported in kitkat. I am using values-hdpi,values-xhdpi, ect to get the UI aligned in different devices.

Please help me find a solution.

1 Answers1

0

Finally after a lot of research I could not find a reliable solution. I don't know if it is the standard way of doing but it served my purpose. I used the answer in the stackoverflow question. get real size of screen

Got the display size of the screen as well using the following

DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

if real width and display width are same it means device does not have a navigation bar, else device has a navigation bar. Accordingly I am setting appropriate layouts. I am concerned only with width as my app only runs in landscape mode.

Community
  • 1
  • 1