How to set screen size in full screen at run time in Android

I want to change the screen layout in my application to full screen when the user clicks the button, but it does not work, my code is :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnFullScreen = (Button) findViewById(R.id.btnFullScreen);
    btnNormalScreen = (Button) findViewById(R.id.btnNormalScreen);

    btnFullScreen.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setTheme(R.style.AppBaseThemeFullScreen);

        }
    });

    btnNormalScreen.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setTheme(R.style.AppBaseTheme);

        }
    });



}

And my Full Screen theme :

<style name="AppBaseThemeFullScreen" parent="android:Theme.Light">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

And my usual topic is

<style name="AppBaseTheme" parent="android:Theme.Light">
    <item name="android:windowNoTitle">true</item>
</style>

So, if there is a way to do this, please help me.

+1
source share
2 answers
void toggleFullScreen(boolean goFullScreen){   
    if(goFullScreen){
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }else{
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    yourView.requestLayout();
}
+10
source

If you want to change the theme from the code, it seems you need to do this before calling setContentView().

You can also try:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
0
source

Source: https://habr.com/ru/post/1599744/


All Articles