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:
When I pressed back to dismissed Overlay Activity, the status bar content was come back:
What I wanted is overlay the status bar without remove the content, how should I do it ?
I tried this since you post and came up with this,
In Manifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
inflator.xml
Function works fine , adjust it as your requirements.Add codes to
View
andGone
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 youractivity_main
too.(change it as you want)activity_main.XML
Enjoy
outputs: