Search code examples
androidhidefullscreenstatusbarandroid-4.4-kitkat

Hide status bar in android 4.4+ or kitkat with Fullscreen


I'm using following code for hiding status bar with full screen purpose:

    void HideEverything(){
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            // Remember that you should never show the action bar if the
            // status bar is hidden, so hide that too if necessary.
            ActionBar actionBar = getActionBar();
            actionBar.hide();
        }
    }

It shows Screen in full-screen mode but when I touch status bar it shows status bar. I tried other options to add the full screen and no title bar theme in application and activity level in manifest, but still results are the same... :(

I don't want to show status bar at any step. How to achieve this in android?

Edit :

Android OS version is 4.4.2 (Above code is working on 4.3 but not on 4.4.2)

Update:

Problem has been solved... Answer is written below separately...


Solution

  • 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... :)