Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog with transparent background in Android

How do I remove the black background from a dialog box in Android. The pic shows the problem.

enter image description here

final Dialog dialog = new Dialog(Screen1.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.themechanger); 
like image 221
Programmer Avatar asked Oct 05 '22 07:10

Programmer


People also ask

How to make custom dialog box transparent in android?

So, create a new XML file under res->drawable directory. Above code will generate custom dialog with fully transparent background. The parent element of this file is RelativeLayout. I have set the background color of this relativelayout as the fully transparent with opacity 67.

Is there a color code for transparent?

You can actually apply a hex code color that is transparent. The hex code for transparent white (not that the color matters when it is fully transparent) is two zeros followed by white's hex code of FFFFFF or 00FFFFFF.


3 Answers

Add this code

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

Or this one instead:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
like image 169
Zacharias Manuel Avatar answered Oct 18 '22 00:10

Zacharias Manuel


TL;DR; You just need two things, firstly in your style do something like:

<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>

Secondly, make 100% sure said style gets applied to your dialog (maybe by passing to constructor).

Full example

<style name="NewDialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:background">@android:color/transparent</item>
</style>

Use in Java:

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

I hope helps you !

like image 105
LongLv Avatar answered Oct 17 '22 23:10

LongLv


I've faced the simpler problem and the solution i came up with was applying a transparent bachground THEME. Write these lines in your styles

    <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
</style>
<style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <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">false</item>
</style>

And then add

android:theme="@style/Theme.Transparent"

in your main manifest file , inside the block of the dialog activity.

Plus in your dialog activity XML set

 android:background= "#00000000"
like image 35
Fahad Ishaque Avatar answered Oct 18 '22 00:10

Fahad Ishaque