Create a transparent dialog on top of activity
Asked Answered
D

2

44

Background

I'm trying to put a layer on top of the current activity which would have explanation of what is going on on the current screen, similar to what occurs on contact+ app .

I know there are some solutions for this (like the showCase library and the superToolTips library ) , and I also know that I can create a view and set it on top by adding it to the window of the activity , but I need put a whole dialog layer on top.

Problem

No matter what I try, each solution doesn't work the way I need it to work.

in short , what I need is:

  • full screen dialog.

  • no change (not visual and not logical) to the action bar, notification bar, and content of the activity behind, meaning that everything behind the dialog stays the same it was shown a moment before the dialog was shown.

  • be transparent except for the views I use for the dialog, which should be shown normally.

what I've tried

Sadly, I've always got only a part of the things I needed.

here's my code:

styles.xml:

<style name="full_screen_dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    </style>

MainActivity.java:

...
final Dialog dialog = new Dialog(this, R.style.full_screen_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.show();

This code will put the layout on top of the activity, but sadly it doesn't have any transparency , even though I've set it . The layout I've used is very simple which is why I don't post it.

Question

what am I missing ? what should be done to fix the code?

how can I make the dialog both transparent, full screen AND that it won't change the action bar and notifications bar.


Working solution

EDIT: after finding a good solution, here's the working code:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.floating_tutorial);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}
Din answered 20/8, 2013 at 14:23 Comment(9)
Brilliant well written, did not have to read anything, just reach the end of the question and there is the answer.. thanksCheckpoint
This dialog I want to display at the center of the screen....how can I do it?Sonja
Got it.... window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);Sonja
@user3138859 I don't understand . SorryDin
@androiddeveloper WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); with this dialog appearing at top.But with wrap content it appears at centerSonja
@user3138859 ok, so I guess you've found how to handle it. :)Din
It would be helpful if you could add the R.layout.floating_tutorial code to this postTilghman
@Tilghman No, because you can do what you wish in the layout. It's irrelavant. You choose what to put there.Din
Great solution givenPointless
C
56

Just change the background color of your Dialog:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Edit:

This prevents the dim effect:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Cajolery answered 20/8, 2013 at 14:53 Comment(7)
easy and short solution. it worked. however, for some reason the screen get darkened when the dialog is shown. please tell me how to change it (customize and/or disable it).Din
oops deleted my comment. if you delete yours, i will delete mine... :) anyway, thank you. it worked perfectly.Din
@androiddeveloper np :) this is really annoying customizing because it's well hidden in the frameworkCajolery
which of the things i've written i don't need anymore? i think full_screen_dialog isn't needed. are there more?Din
The only code which looks suspicous to me is the dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);Cajolery
thank you. it works well. i will now update my question to have the working solution.Din
This approach helped me fix transparency on a Presentation for secondary display. But for some reason, for me it worked only using null instead of ColorDrawable...Lavalley
M
4

Just add this, it really works!

<item name="android:windowIsTranslucent">true</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:windowBackground">@android:drawable/alert_dark_frame</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
Myeloid answered 30/6, 2015 at 3:34 Comment(2)
This was a long time ago. I don't even remember this question.Din
this can be useful for other people, you are not the only person who might want to implement such a windowTilghman

© 2022 - 2024 — McMap. All rights reserved.