visit
With Android 9 (API level 28), Google officially started supporting what’s famously known as the notch, a cutout display at the top (because the last two years were the years of the notch?). I can’t claim that but with the notch support, most of the brands came out with their version of a cutout display and with that, we as developers need to think about yet another edge case, especially if we’re working with a completely immersive experience.
2. Select an Empty Activity Project Template. This would create an empty screen with an Action Bar.
implementation 'com.google.android.material:material:1.1.0'
Note: At the time of writing, the latest Material version was 1.1.0, but you can use any latest stable release you want from or .
1. Open styles.xml.
2. Replace your parent theme from Theme.AppCompat.Light.DarkActionBar to Theme.MaterialComponents.NoActionBar.Bridge (running the app at this stage should not display any toolbar).
3. Open activity_main.xml.
4. Add the following lines in place of the placeholder TextView to add a toolbar (running the app at this stage should display an empty toolbar).
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
5. Open MainActivity.kt.
6. Add this line after setContentView() to setup our own toolbar and display the app title.
setSupportActionBar(findViewById(R.id.toolbar))
1.Open styles.xml.
2. Add a new style below the AppTheme:
<style name="AppTheme.Fullscreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="p">shortEdges</item>
</style>
3. Open AndroidManifest.xml
4. In the MainActivity attributes, add the android:theme attribute with the styles that you just created:
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
5. Now in your MainActivity.kt, add the following lines:
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemUIAndNavigation(this)
}
}
private fun hideSystemUIAndNavigation(activity: Activity) {
val decorView: View = activity.window.decorView
decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
But there must be a way the height of the status bar is set by the system, you must think. Yes there is, and you access it by @android:dimen/status_bar_height but since that’s a private value, adding a android:layout_marginTop=”@android:dimen/status_bar_height” to your toolbar throws a build error:
AAPT: error: resource android:dimen/status_bar_height is private.
1. In your MainActivity.kt, add a new method:
@SuppressLint("NewApi")
private fun adjustToolbarMarginForNotch() {
// Notch is only supported by >= Android 9
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val windowInsets = window.decorView.rootWindowInsets
if (windowInsets != null) {
val displayCutout = windowInsets.displayCutout
if (displayCutout != null) {
val safeInsetTop = displayCutout.safeInsetTop
val newLayoutParams = toolbar.layoutParams as ViewGroup.MarginLayoutParams
newLayoutParams.setMargins(0, safeInsetTop, 0, 0)
toolbar.layoutParams = newLayoutParams
}
}
}
}
2. Call this method just right below hideSystemUIAndNavigation(this) in onWindowFocusChanged to let the change affect when we hide the system UIs.
Note: Since some of the methods that we call here are quite new to the Google APIs (for instance, rootViewInsets, displayCutout, etc), they’re only supported by Android 9. And since the notch is quite a new trend in devices, all the new devices with the notch do run Android ≥ 9, so we’re all good here. 👌
If you run the app now, you see that the margin is only added when we do have a notch:1. In your activity_main.xml, add a new view with a size of the toolbar (?attr/actionBarSize) on top of toolbar, like:
<View
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
Previously published at //proandroiddev.com/android-material-toolbar-vs-displaycutout-6ae99b2b7ef0