28

I have created an AlertDialog using AlertDialog.Builder, but the Dialog border takes up too much space on the screen. How do I remove the border? I have tried using another Activity to emulate the dialog with a transparent background, but the dialog is used repeatedly, and creating a new Activity every time introduces a significant amount of lag.

The answer from here mentions that it can be found in the ApiDemos, but i can't seem to find it.

Community
  • 1
  • 1
kwogger
  • 1,412
  • 1
  • 12
  • 18

8 Answers8

64

Alright, I'll answer my own question. Basically, instead of using AlertDialog.Builder, create a regular Dialog using it's constructor, and use a suitable theme instead of the default Dialog theme.

So your code would look something like this:

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

Hope this helps someone else.

kwogger
  • 1,412
  • 1
  • 12
  • 18
  • 4
    if we do this then dialog.setCanceledOnTouchOutside(true); will have no effect... how to handle this then? – amithgc Aug 13 '10 at 06:50
  • That is exactly what I was looking for. I've been wrestling with the dialog creation system and dozens of badly written tutorials for hours now, and your one line of code was all I needed. Thank you! – kodi Sep 03 '10 at 13:37
  • 3
    but it make the dialog fullscreen..how to prevent it not to be in fullscreen as i write the above code – AndroidDev Nov 18 '11 at 05:32
  • 2
    gist showing custom floating (popup) dialog with no title or border: https://gist.github.com/2643546 – Chris Blunt May 09 '12 at 10:18
33

Here is my solution, to get a dialog that shows only your content.

    Dialog dialog = new Dialog(this,R.style.ThemeDialogCustom);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //you can move the dialog, so that is not centered
    // dialog.getWindow().getAttributes().y = 50; //50 should be based on density

    dialog.setContentView(yourLinearLayout);
    dialog.setCancelable(true);
    //dialog.setOnCancelListener(cancelListener);
    dialog.show();

themes.xml // located in project /res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="ThemeDialogCustom">
    <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:windowBackground">@color/transparent_color</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
   </style>
</resources>

colors.xml // also located there

<?xml version="1.0" encoding="utf-8"?>
<resources>
       <color name="transparent_color">#00000000</color>
</resource>
jokernk
  • 371
  • 3
  • 2
  • 4
    This is a nice solution. It solves the problem perfectly by simply giving you your same dialog, BUT without the border!!! This is a more appropriate solution than the accepted one. – Craig B Jun 15 '13 at 19:05
  • Thanks for correct solution.And also you can add dialog.setCanceledOnTouchOutside(true); to dismiss the dialog on out side dialog touch. – Basavaraj Hampali Jul 10 '13 at 09:33
24

Using android.R.style.Theme_Translucent_NoTitleBar works if you want the dialog to be full screen. An alternative is to create your own style, like so:

<style
    name="Theme_Dialog_Translucent"
    parent="android:Theme.Dialog">
    <item
        name="android:windowBackground">@null</item>
</style>
jonasb
  • 1,847
  • 1
  • 17
  • 15
7

try this :D

 Dialog popUpView= new Dialog(this);
 popUpView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
Ishanssi
  • 69
  • 1
  • 3
2

if you have 2 border you need to use a ContextThemeWrapper, which it will show only one border as you would like :)

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);
douarbou
  • 2,283
  • 1
  • 21
  • 25
2

I added a transparent pixel to drawable and used the following code :

dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix);
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
1

You can ask the builder to enforce inverse background. Worked for me to display a borderless splash screen with a png source.

bung_julio
  • 31
  • 2
0

In your resources file create a xml file named for e.g. null_image.xml, with the following content:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#0000" />

<size
    android:height="1dp"
    android:width="1dp" />

 </shape>

In your java code, fetch the dialog window and set the xml file as the drawable resource, like this: Depending on your context:

Dialog dialog = new Dialog(getContext());
Window window = dialog.getWindow();
window.setBackgroundDrawableResource(R.drawable.null_image);

That's it, enjoy.

greenspand
  • 749
  • 2
  • 8
  • 15