Pages Navigation Menu

Coding is much easier than you think

How to create transparent activity in android

 

Sometimes, we need an activity to some processing but we need not necessarily have a UI.
For this, you can do just not do a setContentView() call. This will show up an activity with the default theme applied.
 
** UPDATE: Android Complete tutorial now available here.
 
But, what if you need a transparent activity? It should be showing what is behind it and still do some stuff. Just follow this post to get it done.

Add the following style In your res/values/styles.xml file (if you don’t have one, create it.) Here’€™s a complete file:

(the value @color/transparent is the color value #00000000 which I put in res/values/color.xml file. You can also use @android:color/transparent in later Android versions)

Then apply the style to your activity, for example:

...

 

Additionally, you can call requestWindowFeature(Window.FEATURE_NO_TITLE) to remove the title bar.

Sometimes, you may need to display a dialog on this activity. Say, you are processing something and want to show the user the progress. For this, you can create a ProgressDialog and show it. But, in case of a transparent activity, it displays an outer box for the dialog.

To get rid of this, in your onCreateDialog or the place where you are initializing the dialog do the following:

@Override
protected Dialog onCreateDialog(int id) {

switch(id) {
case 100:
dialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setMessage("Message");
//The below line does the trick!!
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
return super.onCreateDialog(id);
}