kwogger
kwogger

Reputation: 1422

Android Borderless Dialog

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.

Upvotes: 28

Views: 34751

Answers (8)

greenspand
greenspand

Reputation: 749

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.

Upvotes: 0

douarbou
douarbou

Reputation: 2283

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);

Upvotes: 2

Ishanssi
Ishanssi

Reputation: 69

try this :D

 Dialog popUpView= new Dialog(this);
 popUpView.getWindow().setBackgroundDrawable(new ColorDrawable(0));

Upvotes: 7

jokernk
jokernk

Reputation: 371

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>

Upvotes: 33

TomBombadil
TomBombadil

Reputation: 71

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

dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix);

Upvotes: 2

bung_julio
bung_julio

Reputation: 31

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

Upvotes: 1

jonasb
jonasb

Reputation: 1875

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>

Upvotes: 24

kwogger
kwogger

Reputation: 1422

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.

Upvotes: 64

Related Questions