Height of status bar in Android
this question was answered before...
Height of statusbar?
Update::
Current method:
ok, the height of the status bar depends on the screen size, for example in a device
with 240 X 320 screen size the status bar height is 20px, for a device with 320 X 480 screen size the status bar height is 25px, for a device with 480 x 800 the status bar height must be 38px
so i recommend to use this script to get the status bar height
Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop =
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
Log.i("*** Elenasys :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
(old Method) to 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;
}
What is the proper way to get status bar height in compose?
So i ended up making a workaround alongside with a modifier extension for easier use.
First, make a data class to hold the fixed inset values.
data class FixedInsets(
val statusBarHeight: Dp = 0.dp,
val navigationBarsPadding: PaddingValues = PaddingValues(),
)
Second, create the extension and a composition local provider.
val LocalFixedInsets = compositionLocalOf<FixedInsets> { error("no FixedInsets provided!") }
@Composable
fun Modifier.fixedStatusBarsPadding(): Modifier {
return this.padding(top = LocalFixedInsets.current.statusBarHeight)
}
@Composable
fun Modifier.fixedNavigationBarsPadding(): Modifier {
return this.padding(paddingValues = LocalFixedInsets.current.navigationBarsPadding)
}
Finally, provide the fixed inset values at the very root of your composable function.
val systemBarsPadding = WindowInsets.systemBars.asPaddingValues()
val fixedInsets = remember {
FixedInsets(
statusBarHeight = systemBarsPadding.calculateTopPadding(),
navigationBarsPadding = PaddingValues(
bottom = systemBarsPadding.calculateBottomPadding(),
start = systemBarsPadding.calculateStartPadding(LayoutDirection.Ltr),
end = systemBarsPadding.calculateEndPadding(LayoutDirection.Ltr),
),
)
}
CompositionLocalProvider(
values = arrayOf(
LocalFixedInsets provides fixedInsets,
),
) {
MainScreen()
}
Navigation bar padding example:
Text(
text = "Hello i'm a text with navigation bar padding",
modifier = Modifier
.fillMaxSize()
.background(color = Color.Red)
.fixedNavigationBarsPadding(),
)
Or you can access the sizes in Dp
directly from the local composition by using calculateXPadding()
:
val fixedInsets = LocalFixedInsets.current
Text(
text = "The navigation bar's height is: ${fixedInsets.navigationBarsPadding.calculateBottomPadding()}",
)
Height of statusbar?
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;
}
Get status bar height dynamically from XML
I have already found the answer:
The recommended way is to use the WindowInsets API, like the following:
view.setOnApplyWindowInsetsListener { v, insets ->
view.height = insets.systemWindowInsetTop // status bar height
insets
}
Accessing the status_bar_height, as it's a private resource and thus subject to change in future versions of Android, is highly discouraged.
Does getSize() in Android include the height of the status bar?
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();
}
How to get status bar height in Flutter?
var height = MediaQuery.of(context).viewPadding.top;
Note: This will only work on mobile devices because other platforms don't have a dedicated status bar and it will returns 0.0
for them.
Related Topics
How to Check a Certain Data Already Exists in Firestore or Not
What Is the List of Supported Languages/Locales on Android
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget - Android
How to Get Zoom Functionality For Images
How to Disable Orientation Change on Android
"Arrayadapter Requires the Resource Id to Be a Textview" Xml Problems
Google Maps Android API V2 - Interactive Infowindow (Like in Original Android Google Maps)
Notification Not Showing in Oreo
Linear Layout and Weight in Android
Android Check Internet Connection
Communicating Between a Fragment and an Activity - Best Practices
Sending and Receiving Sms and Mms in Android (Pre Kit Kat Android 4.4)
Passing Data Between a Fragment and Its Container Activity
How to Stop Edittext from Gaining Focus When an Activity Starts in Android