How to place content below notch with no status bar in Android

550 Views Asked by At

I'm trying to place all the content of my app below a devices notch but no matter what I try the status bar always appears like in this screenshot: imageOfIssue

In my MainActivty I have the following code

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER);

My activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#72bcd4"

    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:textSize="24dp"
        android:background="#EC3BAB"
        android:text="Quizzer Gamer"
        android:paddingTop="14sp"
        android:paddingBottom="14sp"
        android:layout_alignParentTop="true"
        android:textColor="#ffffff"
        android:gravity="center"
        android:layout_centerHorizontal="true"  />

</RelativeLayout>

And finally here is my themes.xml file

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    </style>
</resources>
1

There are 1 best solutions below

0
Tanmay Agarwal On BEST ANSWER

Step 1 is to first hide your status bar using this code in your mainactivity

View decorView = getWindow().getDecorView();
              // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

For example :-

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_main);


    View decorView = getWindow().getDecorView();
              // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);


}

Step 2 is go to your activity theme and write this code:-

  <item name="android:windowLayoutInDisplayCutoutMode">
        shortEdges
    </item>

For example :-

<resources xmlns:tools="http://schemas.android.com/tools">

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

    <item name="android:windowLayoutInDisplayCutoutMode">
        shortEdges
    </item>

</style>

Step 3:- Solve all the red lines error in theme file by hovering mouse and clicking on the errors, a) override resource in value v27 and b) suppress with tools: target API attribute, then your problem will be solved and your app will look like this :-

enter image description here