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.
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"
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));
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();
}
}
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:
You might be better off building a DialogFragment and customize the dialog with setcontentview.
http://developer.android.com/reference/android/app/DialogFragment.html
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
and import android.graphics.drawable.ColorDrawable; on top :)