Get rid of white screen at startup - android

I've been searching online for the right way to avoid the white screen - replacing it with a splashscreen - when my Android app launches.
I found solutions that uses styles like
<style name="AppTheme.SplashTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
But in my case this only delays the view appearance, acting as the app doesn't work (the view inflates after a couple of seconds). I do not want some view that lasts for some seconds I've decided, I just want to replace the blank screen to something custom while my app needs to load.
The only way to have the desired behaviour was to place a drawable as a background in the style of my SplashscreenActivity.
<item name="android:windowBackground">#drawable/background_splash</item>
Now, I would like not to place an image as a background-because of the different resolutions of screens- but to set an xml layout.
Is this possible? What's the best way to get rid of the blank screen without delaying the view appearance?

You should do it as follows
create an xml file in res/drawable folder with below content. This will act as your splash screen. Let's name it splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item>
<bitmap android:src="#drawable/splash_logo"
android:gravity="center" />
</item>
</layer-list>
Then you need to specify a style/theme without titlebar for your splash view, so add below style to your style.xml file.
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash</item>
</style>
In AndroidManifest.xml change theme of your launcher activity to use SplashTheme
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Finally in that launcher activity (in my example MainActivity) change the theme back to default application theme, usually named AppTheme
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState);
...
}
That's it! Now run your application and see that there is no white screen anymore.
References: I used this post and this one to provide the above code example.

Related

Enter full-screen during splash-screen activity

I am currently trying to go full-screen during my splash-screen Activity. I am doing that with a style, which is set on the Activity like this:
style.xml
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
AndroidManifest.xml
<activity
android:name=".Activities.SplashScreenActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You might suggest doing that programmatically, but I already tried all the answers from this, this, this, this, this and this link. Currently, none of the programmatic solutions work for me - put every suggested fix in either onResume or onCreate of my splashActivity.java, but the status and navigation bar were still visible. However, when using my custom style it hides only the status bar, the navigation bar stays visible. What I've tried so far in the xml files: used different parents of the style, used no background drawable, removed screenOrientation and noHistory from activity. I am pretty sure I'm missing something here, but I can't see what. Can share more code if needed. Thanks in advance!
Here my SplashScreenActivity style:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_logo_drawable</item>
</style>
I only use the attribute windowBackground

blank screen comes before splash

The main problem is the splash screen appears after 2-3 seconds. Before splash screen a blank layout appears which I don't want. Otherwise it runs fine. Just want to remove the blank layout which appears before the splash page.
MainActivity:
public class MainActivity extends Activity {
private static String TAG = MainActivity.class.getName();
private static long SLEEP_TIME = 5; // Sleep for some time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.activity_main);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(MainActivity.this, Login.class);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
}
}
}
main layout
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/splash"
tools:context=".MainActivity" >
</RelativeLayout>
Generally speaking, splash screens are not recommended for an app but if you really must.
Android will load a blank layout before it loads an activity layout based on the theme you have set for it. The solution is to set the theme of the splash activity to a transparent one.
Create a transparent theme in res/values/styles.xml
<?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 set the theme in your manifest
<activity android:name=".SplashActivity" android:theme="#style/Theme.Transparent">
...
</activity>
It's better to use a Themed background for your starting activity but if you don't want the blank screen appears before launching main activity you can define your activity like this:
Add android:windowDisablePreview to your AppTheme in res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme">
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
Then set your activity theme in your manifest:
<activity android:name=".MainActivity" android:theme="#style/AppTheme">
...
</activity>
P.S: Setting android:windowDisablePreview has no effect on your activity background, so you have nothing to worry about.
The problem you are facing here is called 'cold start' and it's the time mostly spent in your Application.onCreate method, which usually does some initialisations and it might take longer than you wish. You can read here the official documentation: https://developer.android.com/topic/performance/launch-time.html
If this is the case than setting the theme to translucent or disabling preview as others suggested will only apparently solve the issue, because in reality you will try to launch the app and you won't receive any feedback about the fact the you tapped the app icon. You will see your app starting with a delay, which is not a desired UX.
Blank, black or white screen, it really depends on the android:windowBackground attribute specified in your main activity theme.
The best you can do, apart from moving some of the initialisations that you probably are doing in your Application.onCreate method, is to brand your preview window with a logo as suggested in this post:
https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd
You can go further and improve the user experience by animating your logo image in a splash screen, if it's the case, or by designing the preview window in a way that can be smooth transitioned to your main activity content, like described here:
http://saulmm.github.io/avoding-android-cold-starts
Same question is answered correctly here: https://stackoverflow.com/a/40482549/7094200 and described in this blog post: https://www.bignerdranch.com/blog/splash-screens-the-right-way/
With support for AppCompatActivity
res/values/styles.xml
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowDisablePreview">true</item>
</style>
GL
Source
Really! the below link work for me!!!!
https://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/
write the separate style in style.xml for your splash screen so that you can co-relate both the screen.
Herein the style:
<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">#drawable/splash_final</item>
</style>
It' s android features. You can change background color of your blankscreen. Use style:
<resources>
<style name="Theme" parent="android:style/Theme" />
<style name="Theme.MainTheme" parent="Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/background_drawable</item>
</style>
</resources>
then use it in manifest:
<application
android:name="#string/app_name"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.MainTheme"
>
I think this is similar to some answers that have been posted above. I still would like to recommend the following Big Nerd Ranch guide on how to create a splash screen the right way because its well illustrated and easy to follow. You should really go there and read it instead of continuing below :).
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
But in short what it suggests is, at app start you launch an initial splash activity. For this activity you create an XML drawable (#drawable/background_splash).
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
</layer-list>
Then you create a theme for splash activity’s and set the drawable you created above as its window background.
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
And then in the manifest you tell the splash activity to use the above theme.
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Finally in your splash activity implement theonCreate method like below (thats all the code you need in the activity). This will launch your main activity and finish the splash activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
Add this in your styles file
<item name="android:windowDisablePreview">true</item>
I fixed mine by just updating the build tools in Android Studio.
Add this style
<style name="Theme.Transparent" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
If you load image in your splash screen than load Image from your layout,
like below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="#color/color_white">
<ImageView
android:id="#+id/ivSplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/SPLASH_IMAGE"
/>
</LinearLayout>
Sinle line solution. working perfect in my case;
just add this line in your app theme -
<item name="android:windowBackground">#drawable/splash</item>

Android - When opening activity the opener disappear before the transition animation ends [duplicate]

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.

How to launch a dialog when receiving a gcm notification? [duplicate]

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.

How do I create a transparent Activity on Android?

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.

Categories

Resources