Galaxy S8: screen height returning wrong value - android

This is how we're getting screen height:
getResources().getDisplayMetrics().heightPixels;
The problem we're having is that with the Galaxy S8's hidden nav bar, the value returned is the height minus the nav bar size (even though the nav bar is hidden by the user). How do we get the full usable height?
example: screen height is 2220 pixels but the value returned is 2076
The answer needs to be able to work on Galaxy S8 with or without the nav bar hidden as well as other devices
thank you so much in advance

Try to use this :
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Point is a class from android.graphics package.
From docs : https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
Gets the real size of the display without subtracting any window decor
or applying any compatibility scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell wm
size).
Edit :
I checked this code on LG G4 and it works fine :
//get the full height of device
Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Resources resources = getResources();
int navBarId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int fullHeight = displaySize.y;
//get nav bar size
int navbarSize = resources.getDimensionPixelSize(navBarId);
int availableSize = fullHeight;
//check is navbar is visible
boolean navBarVisible = (getWindow().getDecorView().getSystemUiVisibility() &
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
if(navBarVisible){
availableSize -= navbarSize;
}
Also you can use this code :
view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int i) {
Log.d("Nav bar visible : ", String.valueOf((i & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)==0));
}
});
to check when nav bar is hiding.

I had the same problem and could solve this 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

Related

Does getSize() in Android include the height of the status bar?

Is there a way to tell if getSize() includes the height of the status bar? From testing on a few different phones it seems it does on some but not on others and it is frustrating when trying to make everything in the layout line up properly.
After spending a week on this problem and checked Google's documentation & many other posts here on Stackoverflow (about getting the screen size in pixels via getSize(), getRealSize(), getMetrics(DisplayMetrics metrics), getRealMetrics(DisplayMetrics metrics), etc ... I still have no clue about it.
This picture is giving a better picture of the problem.
On some phones and according to the above picture, the line 'Y/2' should be exactly at size.y / 2 (exact half of the screen between the navigation & the status bars), but is actually not in the middle : The 'Not X' distance on my picture is therefore equal to 'X - getStatusBarSize()'.
I have to add that i am using a ring object with an innerRadiusRatio 2.1 in XML.
But i don't think it's the reason because once i am using the code below, it works on phones where getSize() seems to include the height of the status bar :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Screen dimensions
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
//Get the screen dimensions
maxWidth = size.x
maxHeight = size.y - getStatusBarSize();
}
private int getStatusBarSize() {
Resources resources = getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
While it works simply with maxheight = size.y on others phones (where it does not include the status bar height).
Thanks in advance guys for any help !
I was facing to the same issue more or less and this could be helpful :
// Get the size between the status & the navigation bars
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Real size of the screen (full screen)
Point realSize = new Point();
Display realDisplay = getWindowManager().getDefaultDisplay();
realDisplay.getRealSize(realSize);
//Get the screen dimensions
maxWidth = size.x;
maxHeight = size.y;
if (realSize.y - getNavBarSize() == size.y || realSize.y == size.y) {
Log.d(TAG, "onCreate: getSize() includes the status bar size");
maxHeight = size.y - getStatusBarSize();
}
I've had the same question and this is how I solved it.
Display.getSize() only sometimes includes the status bar height.
For the solution with .getRealSize() you also need the navigation bar height.
With the following solution you don't need the navigation bar height:
View contentView = requireActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int height = contentView.getHeight() - statusBarHeight - actionBarHeight;
(You might have to get the height in contentView.post() if it returns 0.)

Textsize to fill screen width

I have countdown functionality in my app.
Every second a timer sets the current time to a TextView.
So strings are: "56:05","56:04","56:03","56:02"...
I want to make the text size as big as possible
Therefore I've written the following code.
private void measureAndSetText(String text) {
Paint pMeasure = new Paint();
Integer iWidth = _tvContent.getWidth();
Float maxTextSize = 1000f;
pMeasure.setTextSize(maxTextSize);
pMeasure.setFakeBoldText(true);
Float fCurrentWidth = pMeasure.measureText(text);
while (fCurrentWidth > iWidth) {
pMeasure.setTextSize(maxTextSize -= 1);
fCurrentWidth = pMeasure.measureText(text);
}
_tvContent.setText(text);
_tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
}
This code seems to work on my Note2, Lg Optimus 4x HD, Galay ACE and some others.
But not on my Xperia Z in landscape mode.
I guess the reason is the Full Hd Display but I don't understand why.
On the Xperia Z just the ":" sign is displayed in landscape mode. So I think the text is wrapped but I don't know why.
It would make sense to me if the text size I set to it is higher than the screen height (in landscape mode this is actually screen width -> 1080) but this isn't the case.
When I try to set a longer text -> "asdfasd asdf" it is correctly displayed.
Can someone point me to the problem?
Cheers,
Stefan
UPDATE:
I figured out that my iWidth variable which holds the width of my TextView (_tvContent.getWidth()) has the value 1770.
How can that be since my Xperia Z has just 1920x1080???
So I thought that it may be a failure in TextView.getWidth() and added the following code:
Display display3 = getWindowManager().getDefaultDisplay();
Point size3 = new Point();
display3.getSize(size3);
int width = size3.x;
Display display1 = getWindowManager().getDefaultDisplay();
int width1 = display1.getWidth(); // deprecated
int height1 = display1.getHeight(); // deprecated
Display display = getWindowManager().getDefaultDisplay();
Integer iWidth = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Results:
Point(1080, 1776)
Display id 0: DisplayInfo{"Integrierter Bildschirm", app 1080 x 1776, real 1080 x 1920, largest app 1794 x 1701, smallest app 1080 x 1005, 60.0 fps, rotation 0, density 480, 442.451 x 443.345 dpi, layerStack 0, type BUILT_IN, address null, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}, DisplayMetrics{density=3.0, width=1080, height=1776, scaledDensity=3.0, xdpi=442.451, ydpi=443.345}, isValid=true
Is this a Bug on site of Sony?
After I implemented my own TextView with its own OnDraw I've seen that there is an error in LogCat: ERROR/OpenGLRenderer(2503): Font size to large to fit in cache.
After a short research I figgured out that this is really a Bug in Android.
Addding this line to my custom TextView did the trick for me:
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

How to get screen size in Android, including virtual buttons?

For normal uses you can use the DisplayMetrics class and get the "renderable" size, but I want to figure out the actual physical screen size, which includes the virtual buttons height.
On my Galaxy Nexus the reported size no matter what I tried is 1196x720, what can I use to get the physical one which is 1280x720 ? Same goes for Nexus 7, Nexus 4 and Nexus 10 devices.
UPDATE : This is the final code I now use, including the fix :
//Activity A = this;
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) A.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
Display disp = wm.getDefaultDisplay();
int API_LEVEL = android.os.Build.VERSION.SDK_INT;
if (API_LEVEL >= 17)
{
disp.getRealMetrics(displayMetrics);
}
else
{
disp.getMetrics(displayMetrics);
}
int Width = displayMetrics.widthPixels;
int Height = displayMetrics.heightPixels;
You should use Display.getRealSize(Point) From the official docs here
The display area is described in two different ways.
The application display area specifies the part of the display that
may contain an application window, excluding the system decorations.
The application display area may be smaller than the real display area
because the system subtracts the space needed for decor elements such
as the status bar. Use the following methods to query the application
display area: getSize(Point), getRectSize(Rect) and
getMetrics(DisplayMetrics).
The real display area specifies the part
of the display that contains content including the system decorations.
Even so, the real display area may be smaller than the physical size
of the display if the window manager is emulating a smaller display
using (adb shell am display-size). Use the following methods to query
the real display area: getRealSize(Point),
getRealMetrics(DisplayMetrics).
The answer in the similar question seems to have a solution to get the real size of the display when API < 17 https://stackoverflow.com/a/15699681/601298
WindowManager w = activity.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
widthPixels = metrics.widthPixels;
heightPixels = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}

Dimension of soft buttons bar

Is there a way to determine the dimension (width) of the menu bar of devices without hard menu buttons? (like the archos devices).
I need to know the usable dimension of the screen...
This method is very useful in order to set the layout padding in Android KitKat (4.4). Using this, you can avoid the soft buttons bar overlapping over your layout.
The getRealMetrics method is only available with API 17 and +, but I'm only using the following method I wrote for devices on API 19+
#SuppressLint("NewApi")
private int getSoftbuttonsbarHeight() {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return 0;
}
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
return realHeight > usableHeight ? realHeight - usableHeight : 0;
}
Tested upon Nexus 5 & Nexus 7 2013.
A very late answer but for anyone that encounters this problem. A simple way to get the amount of usable space offsetting any soft keys/navigation bar or even the status bar, is to use getWindowVisibleDisplayFrame(Rect) from the Apps DecorView. This will load a Rect object with the dimension of the display that is actually usable.
Since you cannot directly get a reference to the Activiy's DecorView, you need to first get the Activity's window using the accessor method, getWindow(). This will return the window object that holds the Activity and other screen elements. To get the decorView that contains the activity as well as the screen decorations i.e. the status bar, navigation bar/soft button bar, call accessor method getDecorView(). Once your have a reference to the DecorView call getWindowVisibleDisplayFrame(Rect) and you will have a loaded Rect object.
Starting Andorid 3.2, the height of system status bar is not included in DisplayMetrics's height, you have to use undocumented APIs (Display.getRawWidth() and Display.getRawHeight()) to get the physical screen width or height.
Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);
UPDATED: For API 13-16, you have to use the above code to get real width/height. For API 17+, you can now use the new public API, Display.getRealSize()
for me, in android note 10, I need to get navigation bar height by dimen:
fun getSoftNavigationBarSize(resources: Resources): Int {
var result = 0
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}

Height of statusbar?

Is there a way to get the height of the statusbar + titlebar? Checking the dev forum shows the same question but no solution (that I could find).
I know we can get it after the initial layout pass, but I'm looking to get it in onCreate() of my activity.
Thanks
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
Get the Height of the status bar on the onCreate() method of your Activity, use this method:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
For those, like me, who want to use it in your XML layouts:
<...
android:layout_marginTop="#*android:dimen/status_bar_height"
... />
Don't be confused by that Android Studio (2.2 Preview 3 -- at the moment of writing) doesn't support this notion. The runtime does. I took it from the Google's source code.
Although this is an old question, I found that the answer didn't work in onCreate():
I found this code from here which does work in the onCreate() method
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
I hope this helps to anyone that runs into this issue.
The supported way of getting status bar height is to use WindowInsets class starting from API 21+:
customView.setOnApplyWindowInsetsListener((view, insets) -> {
// Handle insets
return insets.consumeSystemWindowInsets();
});
or WindowInsetsCompat for support libraries:
ViewCompat.setOnApplyWindowInsetsListener(customView, (view, insets) -> {
// Handle insets
return insets.consumeSystemWindowInsets();
});
You can also override the onApplyWindowInsets method inside the view:
public class CustomView extends View {
#Override
public WindowInsets onApplyWindowInsets(final WindowInsets insets) {
final int statusBarHeight = insets.getStableInsetTop();
return insets.consumeStableInsets();
}
}
For further details, I'd recommend checking Chris Banes talk - Becoming a master window fitter (slides available here).
You could also take the dimension of the status bar found in the dimens.xml file of android using the way that this blog post describes.
This can get the height of the statusbar without the need to wait for drawing.
Quoting the code from the blog post:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
You need to put this method in a ContextWrapper class.
I would suggest next code:
Rect rect = new Rect();
Window window = activity.getWindow();
if (window != null) {
window.getDecorView().getWindowVisibleDisplayFrame(rect);
android.view.View v = window.findViewById(Window.ID_ANDROID_CONTENT);
android.view.Display display = ((android.view.WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//return result title bar height
return display.getHeight() - v.getBottom() + rect.top;
}
Two examples:
1) Device 1
brand: samsung,
device: maguro,
board: tuna,
cpu_abi: armeabi-v7a,
display: ICL53F.M420KREL08,
manufacturer: samsung,
model: Galaxy Nexus,
ver.release: 4.0.2,
ver.sdk: 14;
Screen resolution: 1280 x 720.There are no hardware buttons on this device.
Results:
rect: left=0 right=720 top=72 bottom=1208;
v: left=0 right=720 top=72 bottom=1208;
display: height=1208 width=720;
correct result=72;
Device 1 has title bar at the top of the screen and status bar with software buttons at the bottom of the screen.
2) Device 2
device: bravo,
board: bravo,
cpu_abi: armeabi-v7a,
display: FRG83G,
manufacturer: HTC,
model: HTC Desire,
ver.release: 2.2.2,
ver.sdk: 8,
Screen resolution: 800 x 400. This device has hardware buttons.
Results:
rect: left=0 right=480 top=38 bottom=800;
v: left=0 right=480 top=0 bottom=800;
display: height=800 width=480;
correct result: phone_bar_height=38;
Device 2 has title bar at the top of the screen and hasn't status bar at all.
Two solutions were suggested above:
A) v.getTop() - rect.top
(it is incorrect for device 1 - it gives 0 instead of 72)
B) display.getHeight() - v.getHeight()
(it is incorrect for device 2 - it gives 0 instead of 38)
Variant:
display.getHeight() - v.getBottom() + rect.top
gives correct results in both cases.
Update
3) One more example (third device):
brand: LGE,
device: v909,
cpu_abi: armeabi-v7a,
display: HMJ37,
model: LG-V909,
ver.release: 3.1,
ver.sdk: 12
rect: left=0 right=1280 top=0 bottom=720
v: left=0 right=1280 top=0 bottom=720
Display: height=768 width=1280
phone_bar_height=48
Device 3 has horizontal orientation, hasn't title bar at the top of the screen and has status bar at the bottom of the screen.
So, here:
int size_of_title_bar = rect.top;
int size_of_status_bar = display.getHeight() - v.getBottom();
It's correct for devices 2 and 3. I am not sure about device 1. User sent me screenshot of device 1. There is a status bar with software button there. But expression "display.getHeight() - v.getBottom()" gives 0.
Attach a runnable to one of your views in your onCreate method, and place the above code in there. This will cause the application to calculate the status bar + titlescreen height when they are attached to the screen.
Take a look at the code below:
myView.post(new Runnable() {
#Override
public void run() {
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
}
});
If this still doesn't do the trick, try invoking the view's postDelayed method instead of post and adding a millisecond value as the second argument.
This may seem unrelated but most of the time, the reason that people look for the status bar height is to offset their own views so they are not placed under it.
By setting fitsSystemWindows on the view you want to "push down" to give space to the status bar, it will be done automatically and according to the size of the status bar on each device. Padding will be added to the view that has this property set to true.
Keep in mind that padding will only be added to the first view in the hierarchy with fitSystemWindows set to true
This applies to cases where the status bar is translucent for example. Make sure that you set a Theme to the activity that doesn't have fitSystemWindows set, otherwise the padding will be added to the activity instead (because it's first in the hierarchy).
This article is a good read on the subject
As of API 23 there is a better solution to getting the status bar height. API 23 adds a WindowInsets feature, so you can use this code to get the size of the system insets, in this case at the top.
public int getStatusBarHeight() {
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
return binding.mainContent.getRootWindowInsets().getStableInsetTop();
}
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if(resourceId != 0) {
return getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
Note that getRootWindowInsets() will return null until after the View has been attached to a Window so it can't be used directly in onCreate() but you can add a listener for the window attach and do it there - here I am adding the status bar inset to the size of my toolbar, which I hide and show, along with the status bar. When it's shown, I want the status bar over the top of it so I add the status bar height to the toolbar's top padding.
binding.mainContent.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View view) {
binding.toolbar.setPadding(binding.toolbar.getPaddingLeft(),
binding.toolbar.getPaddingTop() + getStatusBarHeight(),
binding.toolbar.getPaddingRight(), binding.toolbar.getPaddingBottom());
binding.mainContent.removeOnAttachStateChangeListener(this);
}
#Override
public void onViewDetachedFromWindow(View view) {
}
});
I think better way to calculate that is to get height of fullscreen minus our main layout
phoneBarsHeight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight()
- mainView.getHeight();
And you can put it in onGlobalLayout(). This works on tablets too, I tried it on Theme.NoTitleBar, but it must always works.
Maybe you can even enhance it and use it onCreate() by changing mainView.getHeight() to mainView.getMeasuredHeight().
The solution posted by Jorgesys is very good, but it doesn't work inside onCreate() method.
I guess it's because statusbar and titlebar are created after onCreate().
The solution is easy - you should put code inside runnable and execute it after onCreate() by using root.post((Runnable action) );
So the whole solution:
root = (ViewGroup)findViewById(R.id.root);
root.post(new Runnable() {
public void run(){
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** jakkubu :: ", "StatusBar Height= " + StatusBarHeight +
" , TitleBar Height = " + TitleBarHeight);
}
});
I find it here
Ok. Final answer!!! One that does not have side-effects, relies on documented behavior, supports Q, cutouts, devices with different status-bar size depending on orientation, &c.
protected void onCreate(Bundle savedInstanceState) {
...
setContentView(R.layout.activity_main);
....
// Use The topmost view of the activity, which
// is guaranteed to be asked about window insets/
View rootView = findViewById(R.id.root_view_of_your_activity);
ViewCompat.setOnApplyWindowInsetsListener(rootView, new OnApplyWindowInsetsListener() {
#Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
{
//THIS is the value you want.
statusBarHeight = insets.getSystemWindowInsetTop();
// Let the view handle insets as it likes.
return ViewCompat.onApplyWindowInsets(v,insets);
}
});
The callback occurs after onStart(), before first layout, and occasionally thereafter.
Current actual way:
Kotlin:
ViewCompat.setOnApplyWindowInsetsListener(toolbar) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
view.updateLayoutParams<MarginLayoutParams> {
topMargin = insets.top
}
WindowInsetsCompat.CONSUMED
}
Java:
ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
mlp.topMargin = insets.top;
v.setLayoutParams(mlp);
return WindowInsetsCompat.CONSUMED;
}
Google recommends using it like this if you want to support edge-to-edge in your app.
Targeting API 30, I've used successfully updated Nicklas answer (ref:https://stackoverflow.com/a/47125610/2163045)
In my example I'm adjusting dynamically custom toolbar height in fullscreen WindowCompat.setDecorFitsSystemWindows(window, false) Activity
Tested on GooglePixel 5
class MyActivity : ViewBindingActivity<LayoutBinding>() {
...
override fun created(binding: LayoutBinding, savedInstanceState: Bundle?) {
binding.root.setOnApplyWindowInsetsListener { _, insets ->
val statusBarHeight = insets.getInsets(WindowInsets.Type.statusBars()).top // <- HERE YOU ARE
val toolbarHeight = getDimenPx(R.dimen.toolbar_height)
binding.toolbar.layoutParams.height = statusBarHeight + toolbarHeight
insets
}
}
}

Categories

Resources