How to create transparency in your activity or dialog in android – different methods?

By | March 16, 2013

There are different ways for making an acitivity transparent.

First method

1. Through XML in AndroidManifest

like this.

<activity
            android:name="com.coderzheaven.interfacedemo.MainActivity"
            android:label="@string/app_name" 
           <strong> android:theme="@android:style/Theme.Translucent"</strong>
            >
          
        </activity>

Here we are using Android’s built in theme.

Method 2

Through styles

create a style like this in your styles.xml

 <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

and applying like this in the AndroidManifest.xml

 <activity
            android:name="com.coderzheaven.interfacedemo.MainActivity"
            android:label="@string/app_name" 
            <strong>android:theme="@style/Theme.Transparent"</strong>
            >
        </activity>

Check out this post for the complete implementation.
http://www.coderzheaven.com/2011/07/20/how-to-create-a-transparent-activity-in-android/

Now we see how to make a Dialog transparent.

I am using this post for explaining this

http://www.coderzheaven.com/2013/02/13/extend-dialog-class-write-its-button-click-activity-interfaces-android/

package com.coderzheaven.interfacedemo;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.coderzheaven.interfacedemo.MyDialog.myOnClickListener;

public class MainActivity extends Activity {

	public myOnClickListener myListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);

		// create new onclicklistener interface //
		myListener = new myOnClickListener() {
			@Override
			public void onButtonClick() {
				Toast.makeText(getApplicationContext(),
						"This is a Transparent Custom Dialog Demo",
						Toast.LENGTH_LONG).show();
			}
		};

		MyDialog mydialog = new MyDialog(this, myListener);
		mydialog.show();
		addTransparency(mydialog);
		
	}

	void addTransparency(MyDialog mydialog) {
		mydialog.getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
	}

}

This code does the work here for transparency in the Dialog.

void addTransparency(MyDialog mydialog) {
		mydialog.getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
	}

Leave a Reply

Your email address will not be published. Required fields are marked *