50

I want to remove the black background on custom dialog as shown in the picture. I'm sure the black background was from the dialog, not from app's background.

custom dialog with black background around it ;

AlertDialog code

public class MyAlertDialog extends AlertDialog { 
    public MyAlertDialog(Context context) 
    {  
        super(context); 
    }  

    public MyAlertDialog(Context context, int theme) 
    { super(context, theme); }
}

Activity code

public void showMyDialogOK(Context context, String s, DialogInterface.OnClickListener OkListener) {        
    MyAlertDialog myDialog = new MyAlertDialog(context, R.style.MyDialog2);        
    myDialog.setTitle(null); 
    myDialog.setMessage(s);        
    myDialog.setButton(DialogInterface.BUTTON_POSITIVE ,"Ok", OkListener);
    myDialog.show();    
}

Styles

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
            <item name="android:alertDialogStyle">@style/AlertDialog</item>  
        </style>    

        <style name="MyTheme2" parent="@android:style/Theme.Black">
            <item name="android:alertDialogStyle">@style/AlertDialog</item>    
        </style> 

        <style name="AlertDialog">        
            <item name="android:fullDark">@null</item>
            <item name="android:fullBright">@null</item>
            <item name="android:topDark">@drawable/popup_top_dark</item>
            <item name="android:topBright">@null</item>
            <item name="android:centerBright">@null</item>
            <item name="android:centerDark">@drawable/popup_center_dark</item>
            <item name="android:centerMedium">@null</item>
            <item name="android:bottomDark">@null</item>
            <item name="android:bottomBright">@null</item>
            <item name="android:bottomMedium">@drawable/popup_bottom_medium</item>
        </style>

        <style name="MyDialog2" parent="@android:Theme.Dialog">        
            <item name="android:windowBackground">@null</item>    
            <item name="android:buttonStyle">@style/CustomButton</item>  
        </style>    

        <style name="CustomButton" parent="@android:style/Widget.Button">        
            <item name="android:background">@drawable/button_stateful</item>  
        </style>
</resources>

Image resources

popup_center_dark.9.png

popup_center_dark.9.png

popup_bottom_medium.9.png

popup_bottom_medium.9.png

popup_top_dark.9.png

popup_top_dark.9.png

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
pengwang
  • 19,536
  • 34
  • 119
  • 168
  • you have already asked this question in past also right? http://stackoverflow.com/questions/7497449/how-to-remove-rectangle-frame-of-the-custom-dialog – dira Nov 14 '11 at 08:49
  • 1
    the two question not same,the question have resolve solve the before question,but bring new question.before question only want to remove the frame not the background – pengwang Nov 14 '11 at 08:54
  • hi pengwang, can you please share the code for custom dialog or explain how you changed the background an button style of dialog – Shruti Nov 23 '11 at 10:21
  • Not a single answer worked for me. The answer posted here is the right solution. http://stackoverflow.com/a/25174316/786337 – Tarun Nov 25 '14 at 16:07

14 Answers14

104
public MyAlertDialog(
        Context context, 
        int theme
    ) extends AlertDialog { 

    super(context, theme);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
dira
  • 30,304
  • 14
  • 54
  • 69
27

Sonehow getWindow().setBackgroundDrawable() didn't work for me with AlertDialog. I found an easier solution using Dialog. Here's my code -

        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.popup_window);
        dialog.show();
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Khobaib
  • 1,577
  • 3
  • 21
  • 29
25

Try this:

myDialog.getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
droidev
  • 7,352
  • 11
  • 62
  • 94
Charu
  • 1,055
  • 13
  • 18
10

After trying dozens of other solutions for this problem, what ended up working for me is:

<style name="translucentDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

And then setting my dialog to use this theme.

Anson VanDoren
  • 956
  • 1
  • 12
  • 22
6

Remove the background opacity color, you just need to set the DimAmount

dialog.getWindow().setDimAmount(float amount);

The new dim amount, from 0 for no dim to 1 for full dim.

droidster.me
  • 558
  • 8
  • 16
5

Use below two code lines. Tested as well.

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Christopher Nolan
  • 930
  • 1
  • 11
  • 15
1

Following method worked for me:

getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
duggu
  • 37,851
  • 12
  • 116
  • 113
Gaurav Darji
  • 488
  • 5
  • 12
1

//code style in style.xml :

<style name="translucentDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

// in activity :set style to dialog :

   Dialog dialogconf = new Dialog(TreeAct.this, R.style.translucentDialog);
            dialogconf.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                   dialogconf.setContentView(R.layout.dialog_layout);
1

Just change Dialog parent them.

With black backround

<style name="MyDialog2" parent="@android:Theme.Dialog">        

Without black background

<style name="MyDialog2" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
sancho
  • 598
  • 2
  • 8
  • 22
1

you can create xml layout like following and set that layout on dialog(dialog.xml) :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/white_background_bl_aatharv">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true" android:id="@+id/instructions_view">

        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textColor="#FFFFFF"
            android:text="text here " />
    </LinearLayout>
</ScrollView>

here is the code to set layout on alert dialog :

AlertDialog alert = cndtnsbuilder.create();
alert.setView(LayoutInflater.from(
currentactivity.this).inflate(
R.layout.dialog, null));
alert.show();
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Shruti
  • 1
  • 13
  • 55
  • 95
0

To remove the background color, on layout, you just need to set the background to @null

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null">
Pedro Romão
  • 2,285
  • 28
  • 22
0
 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
droidev
  • 7,352
  • 11
  • 62
  • 94
Mani
  • 930
  • 10
  • 14
  • It would be better if you could also describe what is the source of the problem and why this solution should work. – Dev-iL Feb 23 '17 at 15:51
0

I had the same problem with my custom dialog based on the Alertdialog.Builder, which had a black background showing in the title and the body when i use:

builder.setView(rootView)
  .setTitle(dialog_title)
  .setMessage(dialog_mesg)

solution was 1- Use one of the pre-defines alert dialog builder's themes:

  • THEME_DEVICE_DEFAULT_DARK
  • THEME_DEVICE_DEFAULT_LIGHT
  • THEME_HOLO_DARK
  • THEME_HOLO_LIGHT THEME_TRADITIONAL

THEME_DEVICE_DEFAULT_LIGHT worked best for me

2 - set the default dialog button (positive / negative) colors to which ever color you desire like so:

 Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
 Button d = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
 b.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));
 d.setTextColor(ContextCompat.getColor(getActivity(), R.color.primary));

check the below blog post for more detail and tricks to theming options: http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/

tested on Oreo 8.1

Jamal S
  • 1,649
  • 1
  • 19
  • 24
0

In KOTLIN apply this on alert dialog object

window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53