How do I make an activity full screen? I mean without the notification bar. Any ideas?
30 Answers
You can do it programatically:
public class ActivityName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Or you can do it via your AndroidManifest.xml
file:
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
Edit:
If you are using AppCompatActivity then you need to add new theme
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
and then use it.
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>
If you don't want to use the theme @android:style/Theme.NoTitleBar.Fullscreen
because you are already using a theme of you own, you can use android:windowFullscreen
.
In AndroidManifest.xml:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
In styles.xml:
<style name="MyTheme" parent="your parent theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
In AndroidManifest.xml file:
<activity
android:name=".Launch"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <!-- This line is important -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Or in Java code:
protected void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Try this with appcompat from style.xml
. It provides support for all platforms.
<!-- Application theme. -->
<style name="AppTheme.FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" />
Using Android Studio (current version is 2.2.2 at moment) is very easy to add a fullscreen activity.
See the steps:
- Right click on your java main package > Select “New” > Select “Activity” > Then, click on “Fullscreen Activity”.
- Customize the activity (“Activity Name”, “Layout Name” and so on) and click “finish”.
Done!
Now you have a fullscreen activity made easily (see the java class and the activity layout to know how the things works)!
First you must to set you app theme with "NoActionBar" like below
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" />
Then add these lines in your fullscreen activity.
public class MainActiviy extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
It will hide actionbar/toolbar and also statusbar in your fullscreen activity
AndroidManifest.xml
<activity ...
android:theme="@style/FullScreenTheme"
>
</activity>
I. Your main app the theme is Theme.AppCompat.Light.DarkActionBar
For hide ActionBar / StatusBar
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>
<style name="FullScreenTheme" parent="AppTheme">
<!--this property will help hide the ActionBar-->
<item name="windowNoTitle">true</item>
<!--currently, I don't know why we need this property since use windowNoTitle only already help hide actionbar. I use it because it is used inside Theme.AppCompat.Light.NoActionBar (you can check Theme.AppCompat.Light.NoActionBar code). I think there are some missing case that I don't know-->
<item name="windowActionBar">false</item>
<!--this property is used for hiding StatusBar-->
<item name="android:windowFullscreen">true</item>
</style>
To hide the system navigation bar
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
setContentView(R.layout.activity_main)
...
}
}
II. Your main app theme is Theme.AppCompat.Light.NoActionBar
For hide ActionBar / StatusBar
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
<style name="FullScreenTheme" parent="AppTheme">
<!--don't need any config for hide ActionBar because our apptheme is NoActionBar-->
<!--this property is use for hide StatusBar-->
<item name="android:windowFullscreen">true</item> //
</style>
To hide the system navigation bar
Similar like Theme.AppCompat.Light.DarkActionBar
.
thanks for answer @Cristian i was getting error
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
i solved this using
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
-----
-----
}
For Androidx
1. Transparent Statusbar
window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT
2. Transparent Statusbar & Bottomnav bar
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
);
3. Hide Statusbar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
}
else {
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
4. Hide Statubar & Bottomnav bar
val actionBar: ActionBar? = supportActionBar
if (actionBar != null) actionBar.hide()
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
where to put this code ?
override fun onCreate(savedInstanceState: Bundle?) {
/* Put above code here ..... */
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_slider)
}
Note
- I checked this code in Pixel 3A emulator
- Maybe customise android OS not support
- set styel
<style name="Theme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
show Full Immersive:
private void askForFullScreen()
{
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
move out of full immersive mode:
private void moveOutOfFullScreen() {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
I wanted to use my own theme instead of using @android:style/Theme.NoTitleBar.Fullscreen. But it wasn't working as some post on here had mentioned, so I did some tweaking to figure it out.
In AndroidManifest.xml:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
In styles.xml:
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
Note: in my case I had to use name="windowActionBar"
instead of name="android:windowActionBar"
before it worked properly. So I just used both to make sure in the case I need to port to a new Android version later.
Here is an example code. You can turn on/off flags to hide/show specific parts.
public static void hideSystemUI(Activity activity) {
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
Then, you reset to the default state:
public static void showSystemUI(Activity activity) {
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
You can call the above functions from your onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.course_activity);
UiUtils.hideSystemUI(this);
}
KOTLIN
Following the google doc, there is a easy way :
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideSystemUI() }
private fun hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.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) }
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private fun showSystemUI() {
window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) }
Add this in styles.xml
<item name="android:windowFullscreen">true</item>
Example -
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowFullscreen">true</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
And change AndroidManifest file with bellow code
android:theme="@style/AppTheme"
Example -
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"
android:supportsRtl="true">
TIP: Using getWindow().setLayout() can screw up your full screen display! Note the documentation for this method says:
Set the width and height layout parameters of the window... you can change them to ... an absolute value to make a window that is not full-screen.
http://developer.android.com/reference/android/view/Window.html#setLayout%28int,%20int%29
For my purposes, I found that I had to use setLayout with absolute parameters to resize my full screen window correctly. Most of the time, this worked fine. It was called by an onConfigurationChanged() event. There was a hiccup, however. If the user exited the app, changed the orientation, and reentered, it would lead to firing off my code which included setLayout(). During this re-entry time window, my status bar (which was hidden by the manifest) would be made to re-appear, but at any other time setLayout() would not cause this! The solution was to add an additional setLayout() call after the one with the hard values like so:
public static void setSize( final int width, final int height ){
//DO SOME OTHER STUFF...
instance_.getWindow().setLayout( width, height );
// Prevent status bar re-appearance
Handler delay = new Handler();
delay.postDelayed( new Runnable(){ public void run() {
instance_.getWindow().setLayout(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT );
}}, FILL_PARENT_ON_RESIZE_DELAY_MILLIS );
}
The window then correctly re-sized, and the status bar did not re-appear regardless of the event which triggered this.
It worked for me.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
adjustFullScreen(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
adjustFullScreen(getResources().getConfiguration());
}
}
private void adjustFullScreen(Configuration config) {
final View decorView = getWindow().getDecorView();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
Inside styles.xml...
<!-- No action bar -->
<style name="NoActonBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme customization. -->
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#444</item>
<item name="colorAccent">#999</item>
<item name="android:windowFullscreen">true</item>
</style>
This worked for me. Hope it'll help you.
With kotlin this is the way I did:
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_FULLSCREEN
}
}
Immersive Mode
The immersive mode is intended for apps in which the user will be heavily interacting with the screen. Examples are games, viewing images in a gallery, or reading paginated content, like a book or slides in a presentation. For this, just add this lines:
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
Sticky immersive
In the regular immersive mode, any time a user swipes from an edge, the system takes care of revealing the system bars—your app won't even be aware that the gesture occurred. So if the user might actually need to swipe from the edge of the screen as part of the primary app experience—such as when playing a game that requires lots of swiping or using a drawing app—you should instead enable the "sticky" immersive mode.
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
For more information: Enable fullscreen mode
In case your using the keyboard, sometimes happens that StatusBar shows when keyboard shows up. In that case I usually add this to my style xml
styles.xml
<style name="FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
And also this line to my manifest
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/FullScreen">
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
}
After a lot of time with no success I came with my own solution which is quit similar with another developer. So If somebody needs her it is.My problem was that system navigation bar was not hiding after calling. Also in my case I needed landscape, so just in case comment that line and that all. First of all create style
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>
This is my manifest file
<activity
android:name=".Splash"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize"
android:screenOrientation="landscape"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
</activity>
This is my spalsh activity
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash_creen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
And this is my main full screen activity. onSystemUiVisibilityChange thi method is quit important otherwise android main navigation bar after calling will stay and not disappear anymore. Really irritating problem, but this function solves that problem.
public class MainActivity extends AppCompatActivity {
private View mContentView;
@Override
public void onResume(){
super.onResume();
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen2);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.hide();
}
mContentView = findViewById(R.id.fullscreen_content_text);
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener()
{
@Override
public void onSystemUiVisibilityChange(int visibility)
{
System.out.println("print");
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
{
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
else
{
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}
});
}
}
This is my splash screen layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:src="@drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>
</LinearLayout>
This is my fullscreen layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
>
<TextView
android:id="@+id/fullscreen_content_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content2"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
</FrameLayout>
I hope this will help you
https://developer.android.com/training/system-ui/immersive.html
Activity :
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
AndroidManifests:
<activity android:name=".LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_login"
android:theme="@style/FullscreenTheme"
></activity>
Create an empty activity and add two lines in onCreate
.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// full screen activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}
...
}
Use this method after setContentView in onCreate() and pass the Window object by getWindow().
public void makeActivityFullScreen(Window window){
View decorView = window.getDecorView();
// int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
);
}
This code will work for notch screen also. To check the notch fullscreen you require android P but if You have a notch display phone then go to setting-->Display setting -->app display ratio --->select your app --->there will be two options safe are display and full screen , please select the full screen and run the app, you can see the fullscreen in notch also without having android Pie
To make your activity full screen do this:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
//to hide status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
This will hide the toolbar and status bar.
But In some case, you may want to show status bar with a transparent background, in that case, do this:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
// to make status bar transparent
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Some other alternate to hide toolbar instead of
getSupportActionBar().hide()
:
- Remove toolbar by changing the app theme's parent:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
- If you want to remove the toolbar from only one activity then go to manifest, under activity tag add this:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
For kotlin lovers, why not use extension functions:
For first case:
fun AppCompatActivity.makeItFullScreenStatusBarVisible(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
And call this before setContentView
:
makeItFullScreenStatusBarVisible()
For Second One:
fun AppCompatActivity.makeItFullScreenStatusBarHidden(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
And call it before setContentView
:
makeItFullScreenStatusBarHidden()