Related
I want to create a transparent Activity on top of another activity.
How can I achieve this?
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>
It goes like this:
<activity android:name=".usual.activity.Declaration" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
In the styles.xml:
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In the AndroidManifest.xml:
<activity
android:name=".WhateverNameOfTheActivityIs"
android:theme="#style/Theme.AppCompat.Translucent">
...
</activity>
Declare your activity in the manifest like this:
<activity
android:name=".yourActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
And add a transparent background to your layout.
Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
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:
colors.xml
<?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.
I achieved it on 2.3.3 by just adding android:theme="#android:style/Theme.Translucent" in the activity tag in the manifest.
I don't know about lower versions...
2021 facts
Just add
<item name="android:windowBackground">#android:color/transparent</item>
You're done.
windowIsFloating wrong, this makes an INSET floating window.
windowContentOverlay only relates to shadows.
windowIsTranslucent is WRONG, it DOES NOT make it so you can see the activity behind. windowIsTranslucent is only relevant if animating transitions.
backgroundDimEnabled dims the activity below, BUT, it is completely buggy on different devices. (In some cases, it does nothing unless you are using windowIsFloating; in general the behavior is totally buggy/indeterminate.)
colorBackgroundCacheHint is irrelevant except on extremely old devices, the default is null anyway.
In the onCreate function, below the setContentView, add this line:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Just let the activity background image be transparent. Or add the theme in the XML file:
<activity android:name=".usual.activity.Declaration" 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));.
in addition to the above answers:
to avoid android Oreo related crash on activity
<style name="AppTheme.Transparent" parent="#style/Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
<activity
android:name="xActivity"
android:theme="#style/AppTheme.Transparent" />
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.
If you are using AppCompatActivity then add this in styles.xml
<style name="TransparentCompat" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In manifest file you can add this theme to activity tag like this
android:theme="#style/TransparentCompat"
for more details read this article
I just did two things, and it made my activity transparent. They are below.
In the manifest file I just added the below code in the activity tag.
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
It perfectly works for me.
Assign it the Translucent theme
android:theme="#android:style/Theme.Translucent.NoTitleBar"
There're two ways:
Using Theme.NoDisplay
Using Theme.Translucent.NoTitleBar
Using Theme.NoDisplay will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish() in onCreate() (or, technically, before onResume()) will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar, which does not suffer from this limitation.”
Note 1:In Drawable folder create test.xml and copy the following code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
// Note: Corners and shape is as per your requirement.
// Note 2:Create xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="#drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Just add the following line to the activity tag in your manifest file that needs to look transparent.
android:theme="#android:style/Theme.Translucent"
All those answers might be confusing, there is a difference between Transparent activity and None UI activity.
Using this:
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Will make the activity transparent but will block the UI.
If you want a None UI activity than use this:
android:theme="#android:style/Theme.NoDisplay"
You can remove setContentView(R.layout.mLayout) from your activity and set theme as android:theme="#style/AppTheme.Transparent". Check this link for more details.
just put this in style.xml
<item name="android:windowBackground">#android:color/transparent</item>
oR Add in Manifest
<activity android:name=".usual.activity.Declaration"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
The other solutions worked for me but I noticed an issue that may affect some. When I start my activity like this:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Instead of a one-liner like this
startActivity(new Intent(this, MainActivity.class));
I just get black background which is a pain. I haven't figured out what's causing this I'm guessing it is device related (Tested on Xiaomi). So passing values also needs to be like this.
startActivity(new Intent(this, MainActivity.class)
.putExtra("SOME_VALUE", value)
.putExtra("ANOTHER_VALUE", value2));
Edit:
Changing the parent of the style to Theme.AppCompat.Light.NoActionBar seem to have fixed this issue
Along with the gnobal's above solution, I had to set alpha to 0 in the layout file of that particular activity, because on certain phone (Redmi Narzo 20 pro running on Android 10) a dialog portion of the screen was showing with the screen that was supposed to be transparent. For some reason the windowIsFloating was causing this issue, but on removing it I wasn't getting the desired output.
Steps:
Add the following in the style.xml located under res > values > 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>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Set the theme of the activity with the above style in
AndroidManifest.xml
<activity
android:name=".activityName"
android:theme="#style/Theme.Transparent"/>
Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Please Note: You'll have to extend you activity using Activity() class and not AppCompatActivity for using the above solution.
I want to create a transparent Activity on top of another activity.
How can I achieve this?
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>
It goes like this:
<activity android:name=".usual.activity.Declaration" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
In the styles.xml:
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In the AndroidManifest.xml:
<activity
android:name=".WhateverNameOfTheActivityIs"
android:theme="#style/Theme.AppCompat.Translucent">
...
</activity>
Declare your activity in the manifest like this:
<activity
android:name=".yourActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
And add a transparent background to your layout.
Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
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:
colors.xml
<?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.
I achieved it on 2.3.3 by just adding android:theme="#android:style/Theme.Translucent" in the activity tag in the manifest.
I don't know about lower versions...
2021 facts
Just add
<item name="android:windowBackground">#android:color/transparent</item>
You're done.
windowIsFloating wrong, this makes an INSET floating window.
windowContentOverlay only relates to shadows.
windowIsTranslucent is WRONG, it DOES NOT make it so you can see the activity behind. windowIsTranslucent is only relevant if animating transitions.
backgroundDimEnabled dims the activity below, BUT, it is completely buggy on different devices. (In some cases, it does nothing unless you are using windowIsFloating; in general the behavior is totally buggy/indeterminate.)
colorBackgroundCacheHint is irrelevant except on extremely old devices, the default is null anyway.
In the onCreate function, below the setContentView, add this line:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Just let the activity background image be transparent. Or add the theme in the XML file:
<activity android:name=".usual.activity.Declaration" 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));.
in addition to the above answers:
to avoid android Oreo related crash on activity
<style name="AppTheme.Transparent" parent="#style/Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
<activity
android:name="xActivity"
android:theme="#style/AppTheme.Transparent" />
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.
If you are using AppCompatActivity then add this in styles.xml
<style name="TransparentCompat" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In manifest file you can add this theme to activity tag like this
android:theme="#style/TransparentCompat"
for more details read this article
I just did two things, and it made my activity transparent. They are below.
In the manifest file I just added the below code in the activity tag.
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
It perfectly works for me.
Assign it the Translucent theme
android:theme="#android:style/Theme.Translucent.NoTitleBar"
There're two ways:
Using Theme.NoDisplay
Using Theme.Translucent.NoTitleBar
Using Theme.NoDisplay will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish() in onCreate() (or, technically, before onResume()) will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar, which does not suffer from this limitation.”
Note 1:In Drawable folder create test.xml and copy the following code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
// Note: Corners and shape is as per your requirement.
// Note 2:Create xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="#drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Just add the following line to the activity tag in your manifest file that needs to look transparent.
android:theme="#android:style/Theme.Translucent"
All those answers might be confusing, there is a difference between Transparent activity and None UI activity.
Using this:
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Will make the activity transparent but will block the UI.
If you want a None UI activity than use this:
android:theme="#android:style/Theme.NoDisplay"
You can remove setContentView(R.layout.mLayout) from your activity and set theme as android:theme="#style/AppTheme.Transparent". Check this link for more details.
just put this in style.xml
<item name="android:windowBackground">#android:color/transparent</item>
oR Add in Manifest
<activity android:name=".usual.activity.Declaration"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
The other solutions worked for me but I noticed an issue that may affect some. When I start my activity like this:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Instead of a one-liner like this
startActivity(new Intent(this, MainActivity.class));
I just get black background which is a pain. I haven't figured out what's causing this I'm guessing it is device related (Tested on Xiaomi). So passing values also needs to be like this.
startActivity(new Intent(this, MainActivity.class)
.putExtra("SOME_VALUE", value)
.putExtra("ANOTHER_VALUE", value2));
Edit:
Changing the parent of the style to Theme.AppCompat.Light.NoActionBar seem to have fixed this issue
Along with the gnobal's above solution, I had to set alpha to 0 in the layout file of that particular activity, because on certain phone (Redmi Narzo 20 pro running on Android 10) a dialog portion of the screen was showing with the screen that was supposed to be transparent. For some reason the windowIsFloating was causing this issue, but on removing it I wasn't getting the desired output.
Steps:
Add the following in the style.xml located under res > values > 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>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Set the theme of the activity with the above style in
AndroidManifest.xml
<activity
android:name=".activityName"
android:theme="#style/Theme.Transparent"/>
Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Please Note: You'll have to extend you activity using Activity() class and not AppCompatActivity for using the above solution.
I feel like this is listed somewhere and is extremely easy but I cannot seam to find a straightforward answer. How do you set a custom background image. For instance set a PNG as the default background for my app instead of the black screen. Manifest, Layout, Main code thing? An example would be extremely amazing.
xml/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Light"> <!-- Or any other parent you want -->
<item name="android:windowBackground">#drawable/yourcustombackgroundimage.png</item>
</style>
</resources>
and use this style in the manifest file.
<application android:theme="#style/MyTheme">
For those of you that are still looking to do this, the correct way to do it has changed slightly. Piggy-backing off of 500865's answer, here is the adjusted code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:Theme.Holo">
<item name="android:background">#drawable/dummy_background_image</item>
</style>
</resources>
Basically, you have to change windowBackground to just background, you have to include the line about xmlns:android, and you should (although not required) remove the .png extension.
I am trying to customize my styles.xml file to display the same background for each one of my activities.
I tried this code in my styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyTheme" parent="android:Theme.NoTitleBar">
<item name="android:background">#drawable/mybg</item>
</style>
</resources>
this worked pretty well untill I put a TextView in my activity.. Every View I put in my layout get the same background, and that is really annoying
What is the best practice to set only the background of the activity?
I think I have found my answer:
<item name="android:windowBackground">
fits all my needs
May be you can do it like this :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="yourstylename">
<item name="android:background">#drawable/mybg</item>
</style>
and set this style to all your activities root element in xml file.
I want to create a transparent Activity on top of another activity.
How can I achieve this?
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>
It goes like this:
<activity android:name=".usual.activity.Declaration" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
In the styles.xml:
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In the AndroidManifest.xml:
<activity
android:name=".WhateverNameOfTheActivityIs"
android:theme="#style/Theme.AppCompat.Translucent">
...
</activity>
Declare your activity in the manifest like this:
<activity
android:name=".yourActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
And add a transparent background to your layout.
Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
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:
colors.xml
<?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.
I achieved it on 2.3.3 by just adding android:theme="#android:style/Theme.Translucent" in the activity tag in the manifest.
I don't know about lower versions...
2021 facts
Just add
<item name="android:windowBackground">#android:color/transparent</item>
You're done.
windowIsFloating wrong, this makes an INSET floating window.
windowContentOverlay only relates to shadows.
windowIsTranslucent is WRONG, it DOES NOT make it so you can see the activity behind. windowIsTranslucent is only relevant if animating transitions.
backgroundDimEnabled dims the activity below, BUT, it is completely buggy on different devices. (In some cases, it does nothing unless you are using windowIsFloating; in general the behavior is totally buggy/indeterminate.)
colorBackgroundCacheHint is irrelevant except on extremely old devices, the default is null anyway.
In the onCreate function, below the setContentView, add this line:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Just let the activity background image be transparent. Or add the theme in the XML file:
<activity android:name=".usual.activity.Declaration" 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));.
in addition to the above answers:
to avoid android Oreo related crash on activity
<style name="AppTheme.Transparent" parent="#style/Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
<activity
android:name="xActivity"
android:theme="#style/AppTheme.Transparent" />
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.
If you are using AppCompatActivity then add this in styles.xml
<style name="TransparentCompat" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
In manifest file you can add this theme to activity tag like this
android:theme="#style/TransparentCompat"
for more details read this article
I just did two things, and it made my activity transparent. They are below.
In the manifest file I just added the below code in the activity tag.
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
It perfectly works for me.
Assign it the Translucent theme
android:theme="#android:style/Theme.Translucent.NoTitleBar"
There're two ways:
Using Theme.NoDisplay
Using Theme.Translucent.NoTitleBar
Using Theme.NoDisplay will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish() in onCreate() (or, technically, before onResume()) will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar, which does not suffer from this limitation.”
Note 1:In Drawable folder create test.xml and copy the following code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
// Note: Corners and shape is as per your requirement.
// Note 2:Create xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="#drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Just add the following line to the activity tag in your manifest file that needs to look transparent.
android:theme="#android:style/Theme.Translucent"
All those answers might be confusing, there is a difference between Transparent activity and None UI activity.
Using this:
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Will make the activity transparent but will block the UI.
If you want a None UI activity than use this:
android:theme="#android:style/Theme.NoDisplay"
You can remove setContentView(R.layout.mLayout) from your activity and set theme as android:theme="#style/AppTheme.Transparent". Check this link for more details.
just put this in style.xml
<item name="android:windowBackground">#android:color/transparent</item>
oR Add in Manifest
<activity android:name=".usual.activity.Declaration"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
The other solutions worked for me but I noticed an issue that may affect some. When I start my activity like this:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Instead of a one-liner like this
startActivity(new Intent(this, MainActivity.class));
I just get black background which is a pain. I haven't figured out what's causing this I'm guessing it is device related (Tested on Xiaomi). So passing values also needs to be like this.
startActivity(new Intent(this, MainActivity.class)
.putExtra("SOME_VALUE", value)
.putExtra("ANOTHER_VALUE", value2));
Edit:
Changing the parent of the style to Theme.AppCompat.Light.NoActionBar seem to have fixed this issue
Along with the gnobal's above solution, I had to set alpha to 0 in the layout file of that particular activity, because on certain phone (Redmi Narzo 20 pro running on Android 10) a dialog portion of the screen was showing with the screen that was supposed to be transparent. For some reason the windowIsFloating was causing this issue, but on removing it I wasn't getting the desired output.
Steps:
Add the following in the style.xml located under res > values > 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>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Set the theme of the activity with the above style in
AndroidManifest.xml
<activity
android:name=".activityName"
android:theme="#style/Theme.Transparent"/>
Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Please Note: You'll have to extend you activity using Activity() class and not AppCompatActivity for using the above solution.