Android: how to create a transparent dialog-themed activity
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>
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 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:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
and import android.graphics.drawable.ColorDrawable; on top :)
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();
}
}
You might be better off building a DialogFragment and customize the dialog with setcontentview.
http://developer.android.com/reference/android/app/DialogFragment.html
精彩评论