Fullscreen Activity in Android?

后端 未结 30 2212
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 00:12

How do I make an activity full screen? I mean without the notification bar. Any ideas?

相关标签:
30条回答
  • 2020-11-22 00:58

    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

    0 讨论(0)
  • 2020-11-22 00:59

    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

    0 讨论(0)
  • 2020-11-22 00:59

    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);
        }
        ...
    }
    
    0 讨论(0)
  • 2020-11-22 00:59

    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

    0 讨论(0)
  • 2020-11-22 01:00

    For those using AppCompact... style.xml

     <style name="Xlogo" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    

    Then put the name in your manifest...

    0 讨论(0)
  • 2020-11-22 01:01

    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():

    1. Remove toolbar by changing the app theme's parent:

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

    1. 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()
    
    0 讨论(0)
提交回复
热议问题