21

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.

Hacketo
  • 4,978
  • 4
  • 19
  • 34

6 Answers6

36

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

dialog.getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
Mu-ik Jeon
  • 843
  • 7
  • 15
  • Not sure why this is necessary, but appreciate the finding. I just started noticing this on several application and other approaches did not work. – Jay Snayder Oct 15 '14 at 14:27
  • It is works. Thanks a lot! But I hate so much Android workarounds. – TsimoX Feb 01 '15 at 10:58
9

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);
}
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
Ronen A.
  • 101
  • 5
3

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);
}
Hacketo
  • 4,978
  • 4
  • 19
  • 34
  • Thanks @Hacketo for the clue. However the following flags seem to be sufficient for keeping the top area from cutting `getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);` – Rajaraman Subramanian Aug 17 '15 at 17:34
  • For me, only the View.SYSTEM_UI_FLAG_FULLSCREEN flag was necessary (but I'm not trying to hide the navigation bar). Also, for those having issues, try getDialog.getWindow.etc... – BooleanCheese Apr 19 '16 at 18:38
  • It needs to be in onResume because backgrounding the app will cause the dialog to get cut off again otherwise. – BooleanCheese Apr 19 '16 at 18:39
1

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.

Tuomas Tikka
  • 201
  • 6
  • 13
0

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

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

0

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">
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127