1014

I want to create a transparent Activity on top of another activity.

How can I achieve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

24 Answers24

1481

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gnobal
  • 18,309
  • 4
  • 30
  • 35
  • 13
    I used `@android:color/transparent` – Someone Somewhere Oct 19 '11 at 17:32
  • 36
    Great! Just one improvement: If you use parent="@android:style/Theme.Dialog" you will get the exactly behaviour of a dialog. That means fading in/out instead of sliding in/out (like an activity) – PakitoV Dec 09 '11 at 14:11
  • 1
    Will the background be clickable? can I click on the former activity that is shown? – Guy Jan 02 '12 at 09:02
  • 77
    As @Emilio mentioned this will behave like a dialog, mainly because of `android:windowIsFloating` set to true. Remove this property to behave like a normal activity (in this case it will match `android:style/Theme.Translucent.NoTitleBar`) – aromero Jan 03 '12 at 14:44
  • How do I also make it so it's behind the current Activity? It's transparent on top of an Activity at the moment and it can't respond to touch events.. – litterbugkid Mar 11 '12 at 22:14
  • 3
    @Neeta, I don't understand your request. This is an activity even though it's transparent, so it gets its own touch events. Also, what is the benefit of having a transparent activity behind the current one? I think you might be looking for something else. You should probably post what you would like to do as a different question. – gnobal Mar 12 '12 at 11:05
  • @Guy Did you ever figure out the answer to your problem? I'm having the same issue.. – litterbugkid Mar 15 '12 at 08:26
  • How can I round the corners of the activity screen? @marek-sebera – Derzu Jul 18 '12 at 20:34
  • 41
    I removed true to have a fullscreen & transparent activity – Kevin Duong Aug 25 '12 at 09:06
  • @KiemDuong If I remember correctly on older devices android:windowIsFloating was required in order to not show the title of the window. I'm not sure, though. You should test any changes to this code on older devices/emulators if you care about them. – gnobal Aug 26 '12 at 12:21
  • 3
    When I try this code on an android 4.2 device, the entire background is black, instead of transparent. Anyone had that issue? – ch3rryc0ke Feb 01 '13 at 03:50
  • @ch3rryc0ke You're doing something wrong. It definitely works for me on my 4.2 Galaxy Nexus. Maybe your theme is overriding the color values, or maybe you entered wrong values for colors. – gnobal Feb 01 '13 at 05:26
  • 4
    The problem was that I was trying to set the theme programmatically using this.SetTheme(..) in onCreate. For this particular transparent theme, that doesn't work-- you get all black. Once you set the theme in XML, then it works. – ch3rryc0ke Feb 01 '13 at 20:19
  • @gnobal: I was following this question answer to create a transparent activity, My buttons, textviews which I have placed in the top of the xml file which I want to be transparent it comes to the center of the screen when I test it on the device. – NewUser Mar 10 '13 at 08:02
  • @Dibya sorry, but I can't help. You should probably open a new question showing your layout and a screenshot of the issue – gnobal Mar 10 '13 at 14:40
  • 2
    Why not just simply use `android:style/Theme.Translucent.NoTitleBar` ? – xmen Jan 19 '14 at 13:59
  • @xmenW.K. This isn't the same as a transparent activity – gnobal Jan 19 '14 at 15:29
  • @gnobal I used this and it worked great. But I need my screen to be "WAKE UP" which was working fine, but if I apply this style my flags doesn't seems to work: (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) – Laggel May 15 '14 at 19:57
  • @Laggel Sorry, but I can't explain what you're seeing. Maybe see Emilio and aromero comments above – gnobal May 18 '14 at 04:56
  • @gnobal I just commented "true" and the screen is waking up again. – Laggel May 18 '14 at 12:54
  • this indeed makes the activity transparent but also changes the appearance of the views...for example an EditText which normally is an invisible box with a blue line at the bottom becomes a distinct rectangle without the blue line. How do i keep the original style of the views ? – Anonymous Jun 03 '14 at 17:06
  • 2
    I just created a simple project based on this solution on GitHub. You can check it here: https://github.com/jiahaoliuliu/TransparentActivity – jiahao Jul 18 '14 at 11:32
  • 18
    My activity was derived from `AppCompatActivity`. So `parent="android:Theme"` was crashing my app. I just removed it and it worked like charm. Thanks! – Atul Apr 24 '16 at 06:19
  • I can't get a ProgressDialog to appear when I use this... Does anybody know why? I am not calling setContentView either... could that be it? – Nick H May 06 '16 at 00:29
  • it would appear that the actual key is setting `@android:color/transparent` – Fattie Nov 27 '16 at 17:03
  • 2
    It is useful too: 0.9 – DmitryKanunnikoff May 16 '17 at 22:57
  • 3
    @Atul You need to use `parent="Theme.AppCompat"` if your activity extends `AppCompatActivity` – Jaran Mar 15 '18 at 12:04
  • 3
    I removed true to have a fullscreen & transparent activity and if your activity is AppCompactActivity then use Theme.AppCompact if don't want ActionBar then Theme.AppCompact.NoActionBar – Imran Samed May 02 '18 at 10:57
  • 3
    @vishalpatel as you can see, the original answer was written in 2010. It's amazing that it managed to stay relevant for so many years. Why don't you post whatever works for you as the answer. But... make sure it works backwards on all previous versions that are still used by, let's say.. more than 5 percent of the people (like I did :). Good luck! – gnobal Jul 24 '18 at 13:50
  • 3
    If you're using the AppCompatActivity extend `Theme.AppCompat.NoActionBar` otherwise you'll get a title indicating the title in the middle of the screen. – Marvin Mar 25 '20 at 23:15
  • For AppCompatActivity, `parent="Theme.AppCompat.NoActionBar"` – Roon13 Feb 05 '21 at 18:15
  • need help here https://stackoverflow.com/q/66533461/14046751 – android dev Mar 09 '21 at 13:21
  • @Room13 Comment we have to incorporate along with the suggested changes. – a.prateek Mar 22 '23 at 09:50
206

It goes like this:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • could you please guide in bit detail???? i am new to android... where to place it in manifest file and other java files i will create as it is how will i associate it??? – UMAR-MOBITSOLUTIONS Feb 02 '10 at 07:49
  • 3
    You just add the theme as alex has posted to your activity declaration in the manifest - this bit android:theme="@android:style/Theme.Translucent.NoTitleBar, for each Activity you can assign it the Translucent theme – Donal Rafferty Feb 02 '10 at 10:51
  • 3
    can you tell me how can i transparent activity 50%? becase this is 100% and i need 50% – Nikunj Patel Apr 06 '12 at 06:52
  • In the layout xml of the activity that goes on top just use the alpha channel of the color part to set the transparancey level. Ie. #ff000000 would be 100% black and #AA000000 would make it slightly transparent. – slott May 23 '12 at 08:27
  • 17
    @user1129443: `50% black should be #7f000000`. Each component (A, R, G, B) can take values from `0-255`. `50% of 255 = 127. 127 in Hex = 7F` That how to calculate transparency (opacity) – Trung Nguyen Jun 18 '12 at 08:16
  • 6
    This method kind of locks the UI as the activity is running but since it is set to translucent,One cannot do anything.Is there a way to avoid this UI locking. – Akhil Latta Jan 10 '13 at 22:54
  • 4
    @yanchenko Unfortunately this is not a good solution. As Akhil says, it locks up the UI! – likejudo Sep 22 '16 at 02:23
  • This is confusing. what its actually dose is making the activity transparent and block the ui ! If some one looking for activity without ui at all then use this: `android:theme="@android:style/Theme.NoDisplay"` – Kashkashio Jul 30 '19 at 11:14
151

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>
Andrew
  • 36,676
  • 11
  • 141
  • 113
  • 1
    If you are planning on actually displaying something on this Activity (say a Dialog or DialogFragment), you'll notice that everything is Dark themed. So you may want your theme to inherit from `Theme.Appcompat.Light.NoActionBar` instead. – tir38 Oct 29 '15 at 22:05
  • 1
    in my case it showing black background.I have parent theme set something else but on one particular activity I am changing theme as mentioned.Any help? – Abdul Waheed Jan 01 '16 at 06:37
  • 4
    Works great when I remove "android:background" – Oded Breiner Mar 30 '16 at 10:41
  • 2
    i think you want to remove `background` and put your preferred semi transparent colour in `windowBackground` – hmac Aug 24 '18 at 14:33
  • This should be the answer if your activity is using AppCompatActivity in contrast to @gnobal's answer. – Marvin Mar 25 '20 at 23:16
39

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.

Murli Prajapati
  • 8,833
  • 5
  • 38
  • 55
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
29

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" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • this gives IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. – Arpit J. Aug 10 '22 at 08:05
19

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.

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
Gem
  • 1,516
  • 16
  • 21
16

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.

Community
  • 1
  • 1
Camille Sévigny
  • 5,104
  • 4
  • 38
  • 61
16

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 3
    "extremely old devices" - Written 10 years ago, only 2 years after Android launched. I guess devices from 2011 in 2021 must be considered "ancient". lol – Johann Jul 15 '21 at 08:02
  • Your solution doesn't do the thing actually. The accepted answer, it's 10 years old but it works find.. just need to extend AppCompat theme instead of Theme.. – Andranik Aug 06 '21 at 15:33
  • 1
    The accepted answer is quite wrong, for the reasons explained in the five bullet points here. (You can, simply, read the doco for each point to get the facts.) – Fattie Sep 17 '21 at 18:26
  • didn't work, the window ended up with black background. – htafoya Nov 08 '22 at 17:46
  • @htafoya , I have no idea why it doesn't work for some folks. In Android there's an incredible variety of ways you can "start" a project in terms of themes and setup. So unfortunately I cannot say, and I don't think anyone could; the fact that other approaches work (or don't work) for other people unfortunately is also due to a myriad reasons. Android is tough :/ Nevertheless, the five points mentioned here (straight from the recent doco) are factually correct. – Fattie Nov 08 '22 at 18:32
  • nvm, it had to do with the theme name not extending Theme. on the first place – htafoya Nov 08 '22 at 18:36
16

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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
androabhay
  • 655
  • 6
  • 13
9

In the onCreate function, below the setContentView, add this line:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leebeedev
  • 2,126
  • 22
  • 31
8

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" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jian
  • 81
  • 1
  • 1
7

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));.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Drew Leonce
  • 241
  • 3
  • 7
7

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" />
Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28
6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pavel Kataykin
  • 1,527
  • 15
  • 14
5

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

Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
  • bro When i use the Above Code the transparent was Working Perfect But it doesnt display the opacity of the previous Artboard it Display the Bg of my Home Screen ( wallpeper Screen ) [![https://i.stack.imgur.com/rQzd2.jpg][1]][1] [1]: – Karthickyuvan Aug 09 '20 at 13:05
  • here is my code https://stackoverflow.com/questions/63326191/android-how-to-make-a-custom-layout-as-transparency – Karthickyuvan Aug 09 '20 at 13:07
3

I just did two things, and it made my activity transparent. They are below.

  1. In the manifest file I just added the below code in the activity tag.

    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
    
  2. And I just set the background of the main layout for that activity as "#80000000". Like

    android:background="#80000000"
    

It perfectly works for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
2

Assign it the Translucent theme

android:theme="@android:style/Theme.Translucent.NoTitleBar"
Michael Härtl
  • 8,428
  • 5
  • 35
  • 62
saurabh
  • 209
  • 1
  • 4
  • 17
2

There're two ways:

  1. Using Theme.NoDisplay
  2. 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.”

Ebite Zion
  • 340
  • 2
  • 10
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
1

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>
ManiTeja
  • 849
  • 1
  • 9
  • 13
0

Just add the following line to the activity tag in your manifest file that needs to look transparent.

android:theme="@android:style/Theme.Translucent"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
0

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"

Kashkashio
  • 487
  • 5
  • 10
  • Interesting !. In Non UI Activity case, the UI of previous activity which is visible to the user can be interacted with like button clicks, list view swipe etc ? – Ramakrishna Joshi May 04 '23 at 18:54
0

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.

Bali
  • 749
  • 6
  • 19
0

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" />
Mark Nashat
  • 668
  • 8
  • 9
-1

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:

  1. 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>
    

  2. Set the theme of the activity with the above style in AndroidManifest.xml

    <activity
          android:name=".activityName"
          android:theme="@style/Theme.Transparent"/>
    

  3. 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.
Arshak
  • 3,197
  • 1
  • 25
  • 33