Fullscreen Activity in Android?

后端 未结 30 2251
佛祖请我去吃肉
佛祖请我去吃肉 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:53

    Just paste this code into onCreate() method

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    0 讨论(0)
  • 2020-11-22 00:55

    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.

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

    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)       }
    

    Google docs

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

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

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

    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.

    Demo

    0 讨论(0)
提交回复
热议问题