Hide status bar in android 4.4+ or kitkat with Fullscreen

纵饮孤独 提交于 2019-11-28 14:14:07

No solution could work for the problem as Android version is 4.4.2.

As commented by @Academy of Programmer; this solution does not work on Android 6 and above.

But as per the suggestion by Sunny, problem has been solved by preventing the expansion of status bar.

Solution is:

Write an inline customViewGroup class in your main activity.

public class customViewGroup extends ViewGroup {

        public customViewGroup(Context context) {
            super(context);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            Log.v("customViewGroup", "**********Intercepted");
            return true;
        }
    }

Now in your onCreate method of mainActivity add following code before setContentView() :

WindowManager manager = ((WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE));

        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        localLayoutParams.gravity = Gravity.TOP;
        localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                // this is to enable the notification to receive touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;

        customViewGroup view = new customViewGroup(this);

        manager.addView(view, localLayoutParams);

Above code will disable the expansion of status bar. Now make full screen by following code, add it just below the above code and before the setContentView :

//fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

Now in manifest add the permission:

<!-- prevent expanding status bar -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

and you are done.... :)

drawback : This code disables expansion of status bar for the device not just for an activity or an application, so use it if you really need it.

Thank you for all the solutions and suggestions... :)

We cannot prevent the status appearing in full screen mode in kitkat or above devices, so try a hack to block the status bar from expanding.

For an idea, put an overlay over status bar and consume all input events. It will prevent the status bar from expanding.

Using below code you can use full screen ;

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Write below code above setcontentview.

Note : Don't give any theme to this activity.

Hope this helps.

use following code onCreate

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);   
 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!