9

I want to make an overlay screen which overlay entire screen including the status bar and navigation bar.

I spends few hours of Googling/research still no luck.

This is my code:

MainActivity.java (launcher):

package com.blogspot.diannaoxiaobai.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startActivity(new Intent(this, Main2Activity.class));
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.blogspot.diannaoxiaobai.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

Main2Activity.java (launch by MainActivity):

package com.blogspot.diannaoxiaobai.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        setContentView(R.layout.activity_main2);
    }
}

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:background="#99ed3636"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.blogspot.diannaoxiaobai.myapplication.Main2Activity">
    <LinearLayout android:id="@+id/overlay_instruction"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@android:color/transparent"
        android:clickable="true">

    </LinearLayout>
</LinearLayout>

AndroidManifest.xml (Only android:theme="@style/Theme.Transparent" added, the rest were auto generated):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blogspot.diannaoxiaobai.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Main2Activity"
            android:theme="@style/Theme.Transparent"></activity>
    </application>

</manifest>

styles.xml:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="Theme.Transparent" parent="android:Theme.Holo.NoActionBar.Fullscreen">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="colorPrimary">@android:color/transparent</item>
        <item name="colorPrimaryDark">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

</resources>

The screenshot when launched, it did success to overlay entire screen including both status bar and navigation bar, but the status bar content(e.g. clock, battery status) was gone:

enter image description here

When I pressed back to dismissed Overlay Activity, the status bar content was come back:

enter image description here

What I wanted is overlay the status bar without remove the content, how should I do it ?

林果皞
  • 7,539
  • 3
  • 55
  • 70
  • 1
    I'm not sure but when set `windowFullScreen` to false, I get an overlay without pushing away the status bar content. Maybe this is the solution, otherwise, you will have to do [high workarounds](http://stackoverflow.com/a/34061521/2668136) – Blo Dec 27 '16 at 03:27
  • Hi @林果皞 do you want to overlay the status bar as well got a solution for action bar :) – Charuක Dec 27 '16 at 03:54
  • @Fllo yes, set `windowFullScreen` to false fixed it, you saved my life, thanks so much :) You might consider put it as answer. – 林果皞 Dec 27 '16 at 04:30
  • @Charuka Yes, i want to overlay status bar and navigation bar, i.e. overlay entire screen. – 林果皞 Dec 27 '16 at 04:40
  • @Fllo Just note that this doesn't overlay the status/navigation bars' icon white color, e.g. if i change overlay color to solid red color, the icon still visible, but overlay the bar is better than none. – 林果皞 Dec 27 '16 at 05:15

1 Answers1

4

I tried this since you post and came up with this,

In Manifest.xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

 public class MainActivity extends AppCompatActivity {


    private WindowManager wm;
    private ViewGroup mTopView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        applyOverLay();


        mTopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //set your status bar visible Here
                Toast.makeText(MainActivity.this, "Ding Ding ,I got a Click Removing overlay!!", Toast.LENGTH_SHORT).show();
                wm.removeViewImmediate(mTopView);

            }
        });

    }


    public void applyOverLay() {

        //set your status bar gone  Here
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);

        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

        mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.inflator, null);
        getWindow().setAttributes(params);
        wm.addView(mTopView, params);
    }


}

inflator.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="25dp"
    android:background="#56FF0000"
    >
</LinearLayout>

Function works fine , adjust it as your requirements.Add codes to View and Gone status bar in the places that i commented.

Edit: you don't need this, but as you said you are getting null pointer i add this too.There is no use of it for you but only to avoid the error , Its your activity_main not mine. Now if you cant fix it add this as your activity_main too.(change it as you want)

activity_main.XML

<LinearLayout
    android:id="@+id/lin"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:background="#65000000"
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <ImageView
        android:background="#739"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <ImageView
        android:background="#789234"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
</LinearLayout>

Enjoy

outputs:

enter image description here enter image description here

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • I received `java.lang.RuntimeException: here` when run the app. And I comment out `//parentLinearLayout = (LinearLayout) findViewById(R.id.lin);` due to R.id.lin doesn't exist. – 林果皞 Dec 27 '16 at 05:23
  • comeon in your main activity add a linear layout and set id as lin – Charuක Dec 27 '16 at 05:51
  • I get `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference` – 林果皞 Dec 27 '16 at 05:59
  • 1
    wait wait ill update it you have no object like that that's the issue – Charuක Dec 27 '16 at 06:00
  • @林果皞 i update that, its your activiy_main in mine i had two views initialized in my on-create by looking at the names you should be able to add them and fix as you need.But as a help i added the activity_main as well that you set as the content in your activity.Hope you will at least accept now :) – Charuක Dec 27 '16 at 06:06
  • @林果皞 since you got confused with id's i re updated my classes and xml for easy understanding – Charuක Dec 27 '16 at 06:52
  • Thanks, any idea why my windows is not fullscreen ? It happen after applyOverLay(). Screenshot: http://imgur.com/a/I4sGK – 林果皞 Dec 27 '16 at 11:07
  • @林果皞 at least you should do an *upvote* for the work .. muhahaha then i can have a look in to your image. Have you changed anything else.if yes mention.then your device api plz – Charuක Dec 27 '16 at 11:11
  • My device is Huawei P9 Lite and Android version 6.0 – 林果皞 Dec 27 '16 at 11:20
  • @林果皞 any other change you made . eg in order to remove status bar? – Charuක Dec 27 '16 at 11:24
  • If I change `params` to `WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT);` , it become like this http://imgur.com/a/Qfsjg , the background action bar is overlay by status bar. – 林果皞 Dec 27 '16 at 11:35
  • what about `final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);` makes below the status bar i think – Charuක Dec 27 '16 at 11:48
  • Still no luck. I tested in other phone Android 4.4.2 also same. May i know what is your Android version ? I create a new project and get back to you soon. – 林果皞 Dec 27 '16 at 12:01
  • @林果皞 Android 5.1.1 Api 22 umm let me think – Charuක Dec 27 '16 at 12:03
  • need to dig a bit http://stackoverflow.com/a/39977712/5188159 if this one is true it differs with apis need to write if conditions – Charuක Dec 27 '16 at 12:08
  • I create empty activity in Android Studio and replaced with your code, still not full screen. http://imgur.com/a/R2z9X – 林果皞 Dec 27 '16 at 12:24
  • public LayoutParams(int w, int h, int _type, int _flags, int _format) Those are the params that i call by passing those values – Charuක Dec 27 '16 at 12:44