Dialog on Android KitKat seems to be cut-ThrowExceptions

Exception or error:

I used dialog theme for an Activity and it works fine on Android <= 4.3 but not on the latest KitKat 4.4.
Here’s a screenshot of the problem:

dialog android 4.4 problem

Top of the layout is missing.
You can see that the bounds seems to be cut.

I’m using an Android emulator to test the app, so I don’t know if the problem is due to the virtual machine or due to some other reason.

How to solve:

You can solve the problem to be cut from below code.

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

###

You should extend the Dialog and in the onCreate call the following method:

@TargetApi(14)
public void toggleHideBar() {

    if (Build.VERSION.SDK_INT < 18) {
        return;
    }

    // The UI options currently enabled are represented by a bitfield.
    // getSystemUiVisibility() gives us that bitfield.
    int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
    int newUiOptions = uiOptions;
    boolean isImmersiveModeEnabled =
            ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
    if (isImmersiveModeEnabled) {
        Log.d("dialog", "Turning immersive mode mode off.");
    } else {
        Log.d("dialog", "Turning immersive mode mode on.");
    }

    // Status bar hiding: Backwards compatible to Jellybean
    if (Build.VERSION.SDK_INT >= 16 && (newUiOptions & View.SYSTEM_UI_FLAG_FULLSCREEN) <= 0) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    // Immersive mode: Backward compatible to KitKat.
    // Note that this flag doesn't do anything by itself, it only augments the behavior
    // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
    // all three flags are being toggled together.
    // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
    // Sticky immersive mode differs in that it makes the navigation and status bars
    // semi-transparent, and the UI flag does not get cleared when the user interacts with
    // the screen.
    if (Build.VERSION.SDK_INT >= 18 && (newUiOptions & View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) <= 0) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }

    getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}

###

Finally, I tried to do something with the new FullScreen Immersive mode available in Android 4.4. Android FullScreen

Just put this in the onResume of the “dialog” activity, and that allow you to use dialogs in a fullscreen activity, without the top cutting 🙂

Edit : Added a compatibility check for the target version, setSystemUiVisibility is implemented in Api 11, but it seem to need that “patch” only for android 4.4 )

// Only for Api >=4.0 
if (android.os.Build.VERSION.SDK_INT >= 16) {
    int mUIFlag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // I removed this
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; // and this to keep the navigation bar
    getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
}

###

I have a similar problem, but with using a custom dialog. Unfortunately, I couldn’t post my screenshot here.

I tested with a few different options, and looks like running the parent activity in fullscreen mode affects this. If I remove the following code, the dialog seems to work fine.

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

I noticed this after getting the 4.4 KitKat update on a Google Nexus 7 (first version). Also, it doesn’t seem to make a difference if I have the dialog’s title present or not.

###

Just add this line of code in your activity’s onCreate method.

getWindow().setFlags(0x04000000, 0x04000000); // FLAG_TRANSLUCENT_STATUS

###

I had the exact same problem as you describe and show in your screenshots. The problem happened only on KitKat.

In my case it happened on some popup dialogs which used a custom style. Next is a style which reproduces the problem when applie to a dialog. Notice the first line, which has the attribute parent=”android.Theme”, by removing that attribute the problem got solved.

<style name="transparent_low_dim" parent="android:Theme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.3</item>
    <item name="android:windowAnimationStyle">@null</item>
</style>

To fix it I removed the parent attribute so the first line would look now like next:

<style name="transparent_low_dim">

Leave a Reply

Your email address will not be published. Required fields are marked *