Ho to get s8 useable screen android?
Asked Answered
H

2

6

I am noob in android. My problem about useable height of 18:9 devices. When I try to get useable screen in these aspect-ratio my application is woking fine all android devices but when ı compile in Samsung Galaxy s8 it is not working. I am trying to get useable screen of devices. I have already tried method which in these links

https://mcmap.net/q/576080/-how-to-support-18-9-aspect-ratio-in-android-apps

https://android-developers.googleblog.com/2017/03/update-your-app-to-take-advantage-of.html

And I use dynamically

DisplayMetrics metrics = this.getResources().getDisplayMetrics(); width = metrics.widthPixels; height = metrics.heightPixels ;

And I tried

private int getSoftButtonsBarHeight() {
        // getRealMetrics is only available with API 17 and +
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int usableHeight = metrics.heightPixels;
            getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels;
            if (realHeight > usableHeight)
                return realHeight - usableHeight;
            else
                return 0;
        }
        return 0;
    }

And when I try to set params MATCH_PARENT height it is working good. But I need to find useable height pixel to desing my other views proportionally .

Actually these code DisplayMetrics metrics = this.getResources().getDisplayMetrics(); height = metrics.heightPixels ; working in my Activity but when I try to use it in another window which I extend from FramaLayout and add to activity it is not working.

Here is my code block

public class StudentLoginActivity extends Activity {  ...
    FrameLayout.LayoutParams containerParams = new ScrollView.LayoutParams(width, height-sb);
    container = new FrameLayout(this);
    container.setLayoutParams(containerParams);
    container.setBackgroundColor(Color.rgb(248,248,248));
    loginStudentView = new StudentLoginView(this);
    container.addView(loginStudentView); ...

}

   public class StudentLoginView extends FrameLayout { ...
    FrameLayout.LayoutParams cp = new FrameLayout.LayoutParams(width, height);
    setLayoutParams(cp); ...

}

But this problem related with android navigationBar height because when I show navigation bar there is no problem but if I hide navigationBar it is not resize application still working that there is a navigation bar on screen (but I hide the navigationBar).

My problem is very similar this link

android Navigation Bar hiding and persantage of usable screen overlap

Huckster answered 22/8, 2017 at 0:15 Comment(2)
What do you call usable height?Benthos
The second link is wrongly pasted.Benthos
H
2

You can get the useable height (even on Galaxy S8 with or without shown NavBar) with the decorview:

    //Get the correct screen size even if the device has a hideable navigation bar (e.g. the Samsung Galaxy S8)
    View decorView = getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
    Rect r = new Rect();
    decorView.getWindowVisibleDisplayFrame(r);
    int screenHeight = r.bottom; // =2220 on S8 with hidden NavBar and =2076 with enabled NavBar
    int screenWidth = r.right; // =1080 on S8
Harmony answered 19/6, 2018 at 9:57 Comment(0)
S
0

If you have a view that is set to match the parent width and height (the whole screen), you can attach a listener to that view and then get its width and height.

View myView = findViewById(R.id.my_view);
myView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
                int oldBottom) {
            // its possible that the layout is not complete in which case
            // we will get all zero values for the positions, so ignore the event
            if (left == 0 && top == 0 && right == 0 && bottom == 0) {
                return;
            }

           // Do what you need to do with the height/width since they are now set
        }
    });

This answer was taken from here.

Showing answered 22/8, 2017 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.