NullPointerException
NullPointerException

Reputation: 37683

Dialog displaying white background when using a png with transparency as a background

I created a background for a Dialog with Gimp, and it's a PNG with transparency and transparent background. If I display it on a ImageView it's fine and the background is transparent.

<ImageView
    android:src="@drawable/test"

But if I display it as a background of the LinearLayout of my Dialog, then, a white background is displayed instead of the transparency (alpha channel) of the image.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/test"

final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.xmldialog);

This is the PNG:

enter image description here

What should be done to get the alpha channel and not a white background below the image I created when I use as a background of a the LinearLayout of a Dialog? I'm sure it's a problem with the dialog. Something must be setted to true.

Upvotes: 0

Views: 532

Answers (2)

SpiritCrusher
SpiritCrusher

Reputation: 21053

Try to set it png as background of dialog setBackgroundDrawable().

  dialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.ic_launcher));

If you are going with set background to root layout . then set the background as transparent .

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

And make your root layout to hold the background.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/test"

Make sure the png used do not have any colored padding left on borders.

You can also set the Dialog background with specified theme. below is an example .

<style name="DialogTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>

Use this theme in Dialog's constructor.

Dialog dialog = new Dialog(this, R.style.DialogTheme); 

Change the theme as per your need and watch out for attributes used .

Upvotes: 1

Rakshit Sorathiya
Rakshit Sorathiya

Reputation: 679

Two hexadecimal characters can be appended to any hexadecimal color code. The first two characters in an 8-digit hex color code represents its opacity in Android.

The two hexadecimal characters can range from 00 to FF. For example,

-> Normal opaque black hex- "#000000"

-> Fully transparent - "#00000000"

-> Fully opaque - "#FF000000"

-> 50% transparent - "#7F000000"

This way you can change any color to any level of transparency.

To find the hexadecimal prefix from a percentage:

Divide the percentage number by 100 and multiply by 255 to get the decimal value. Convert the decimal to hexadecimal.

For example, for 50%, 50/100 * 255 = 127. Using the link we get hexadecimal value 7F.

Hope this helps you to understand to set the transparent background for any view.

Upvotes: 1

Related Questions