2

I'm trying to get a custom dialog with a transparent background doing this way:

this.getWindow().setBackgroundDrawableResource(R.drawable.transparent);

(where "R.drawable.transparent" is a reference to the color "#00000000")

The weird issue on this is that I can't align my dialog window. It's always aligned to the left, even if I implicit set the Gravity of the window using:

this.getWindow().setGravity(Gravity.CENTER);

And if I just comment the line which set the transparent background, the alignment works fine.

Any help would be much appreciated!

Thanks.

feragusper
  • 1,236
  • 2
  • 11
  • 8

2 Answers2

2

In the strings.xml file, add the following:

<resources>
<color name="transparent">#00000000</color>
</resources>

In the .java file, add the following:

.setBackgroundColor(getResources().getColor(R.color.transparent));
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
1

You need to set the padding on the dialog window.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • I don't want to set paddings or margins, I just want to center the window. Have in mind that I would be needing a relative padding/margin depending on the display screen width and height and that would be a ugly fix :( Is there any problem with the transparent background? I noticed that using a very similar transparent color "#01000000" (just incremented the opacity in 1) it works fine. Android bug? :S – feragusper Feb 23 '10 at 13:47
  • So I had the same problem you are having now, and I did the following: android:layout_margin="20dip" and it fixed it to my satisfaction. Note that I'm using device independent pixels, so it keeps the margin the same visually across different screen resolutions. – Mark B Feb 23 '10 at 14:41