I want to create a transparent Activity on top of another activity.
How can I achieve this?
In my case, i have to set the theme on the runtime in java based on some conditions. So I created one theme in style (similar to other answers):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>
Then in Java I applied it to my activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
if (email != null && !email.isEmpty()) {
// We have the valid email ID, no need to take it from user,
// prepare transparent activity just to perform bg tasks required for login
setTheme(R.style.Theme_Transparent);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
} else
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
}
Remember one Important point here: You must call the setTheme()
function before super.onCreate(savedInstanceState);
. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.
I wanted to add to this a little bit as I am a new Android developer as well. The accepted answer is great, but I did run into some trouble. I wasn't sure how to add in the color to the colors.xml file. Here is how it should be done:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="class_zero_background">#7f040000</color>
<color name="transparent">#00000000</color>
</resources>
In my original colors.xml file I had the tag "drawable":
<drawable name="class_zero_background">#7f040000</drawable>
And so I did that for the color as well, but I didn't understand that the "@color/" reference meant look for the tag "color" in the XML. I thought that I should mention this as well to help anyone else out.
In the onCreate function, below the setContentView, add this line:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Add the following style in your res/values/styles.xml
file (if you don’t have one, create it.) Here’s a complete file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>
(The value @color/transparent
is the color value #00000000
which I put in the 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:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
For dialog activity I use this:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
But you also need to set your main View in the activity to invisible. Otherwise the background will be invisible while all views in it will be visible.
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));
.