19

My original goal here was a modal dialog, but you know, Android didn't support this type of dialog. Alternatively, building a dialog-themed activity would possibly work.
Here's code,

public class DialogActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    setTheme(android.R.style.Theme_Dialog);

    super.onCreate(savedInstanceState);

    requestWindowFeature (Window.FEATURE_NO_TITLE);

    Button yesBtn = new Button(this);
    yesBtn.setText("Yes");
    yesBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DialogActivity.this.finish();

        }

    });

    this.setContentView(yesBtn);
}

However, the background was always black, that really confusing. People got same problem,
http://code.google.com/p/android/issues/detail?id=4394
I just wanna make it look more dialog-like for user. So, how to get this DialogActivity transparent and never cover underlying screen?
Thanks.

This was how I created the style

<style name="CustomDialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
</style>
fifth
  • 4,249
  • 9
  • 45
  • 62

6 Answers6

24

You can create a tranparent dialog by this.

public class DialogActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(" ");
        alertDialog.setMessage("");
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.setButton("Accept", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                
            }
        });
        alertDialog.setButton2("Deny", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertDialog.show();
    }
}

After this just put a line in AndroidManifest.xml, where you define your activity in manifest.

android:theme="@android:style/Theme.Translucent.NoTitleBar"
Community
  • 1
  • 1
user824330
  • 304
  • 2
  • 3
  • 1
    android:theme="@android:style/Theme.Translucent.NoTitleBar" changes the dialog theme, look and feel. Complete UI of dialog is changed due to this. – Smeet Jan 27 '16 at 09:10
  • I think you should use the constructor new AlertDialog.Builder(this, R.style.Your_Dialog_Theme); – javmarina Mar 25 '16 at 19:44
15

The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog" then in the activity's onCreate method call getWindow().setBackgroundDrawable(new ColorDrawable(0));

Drew Leonce
  • 241
  • 3
  • 7
  • 1
    Thanks for your answer. See http://stackoverflow.com/questions/17459289/set-shape-background-to-transparent-in-android/17459644#17459644 – Cote Mounyo Jul 03 '13 at 23:19
6

Just one thing to add to all the answers here. To achieve a full dialog like behaviour, you can reposition the dialog by changing the window layout params:

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;

this.getWindow().setAttributes(params);

We used a similar technique to achieve a floating activity effect in the Tooleap SDK:

floating calculator

Danny
  • 584
  • 6
  • 14
3
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

and import android.graphics.drawable.ColorDrawable; on top :)

Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
Fakhar
  • 3,946
  • 39
  • 35
3

Just a quick update on @user824330 answer. Since you have the AlertDialog.Builder class, why not using it properly? Many methods are now deprecated. The up-to-date code would be similar to this:

public class DialogActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("title")
       .setMessage("message")
       .setIcon(R.drawable.ic_launcher)
       .setPositiveButton("Yes", new OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {}
       })
       .setNegativeButton("No", new OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {}
       });

    final AlertDialog dialog = builder.create();
    dialog.show();
}

}

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
0

You might be better off building a DialogFragment and customize the dialog with setcontentview.

http://developer.android.com/reference/android/app/DialogFragment.html

Bryan
  • 3,220
  • 3
  • 26
  • 31