Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 711
fifth Avatar asked May 20 '11 10:05

fifth


People also ask

How do you make a transparent background on activity?

3 by just adding android:theme="@android:style/Theme. Translucent" in the activity tag in the manifest. This works fine for 2.2 also.

What is transparent activity?

In Android, we can create a transparent activity that will not be visible but your application will be running. The best part of this transparent activity is that you can create a transparent activity by just changing the resource file and we need not write the java or kotlin code for the same.


2 Answers

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"
like image 135
user824330 Avatar answered Oct 14 '22 09:10

user824330


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

like image 33
Drew Leonce Avatar answered Oct 14 '22 11:10

Drew Leonce