18

I'd like to have an activity (2) with translucent aspect over another activity (1), aligned at the top of the screen (4).

enter image description here

I have tried assigning these themes to activity number 2:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/black</item>
</style>  

<style name="CustomTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
</style>    

But the result is always 3.

If I set <item name="android:windowIsFloating">false</item> in the CustomTheme the result is 2.

Can anybody tell me how can I get 4? Thanks!

UPDATE: This is my activity 2 layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#0000">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#FFFFFF">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>
Jonik
  • 80,077
  • 70
  • 264
  • 372
Xavi Gil
  • 11,460
  • 4
  • 56
  • 71

5 Answers5

28

Finally, this theme worked to get a result like image number 4:

  <style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:background">@android:color/transparent</item>
 </style>  

In my activity 2 layout, I could eihter set android:background="@android:color/transparent" or not set any value at all to make it work.

Thanks to MikeIsrael and Veer for their help.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
  • I just did what you wrote and it doesn't work. I have one style like your last one, the one in the answer. 2 activities, and something is missing because it is not transparent :( – Taochok Aug 09 '16 at 08:40
11

I've read the other solutions, but here is my solution:

style.xml

<resources>
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</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>
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light">
    <item name="android:background">@android:color/transparent</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>

AndroidManifest.xml

<activity
    android:name=".LoginActivity"
    android:theme="@style/mytransparent.windowTitle"
    android:configChanges="orientation"
    android:label="@string/title_activity_login"
    android:screenOrientation="portrait" ></activity>
uday
  • 1,348
  • 12
  • 27
arm
  • 111
  • 1
  • 2
5

If you use AppCompatActivity then you should use as parent Theme.AppCompat otherwise application can hang or crash with error (java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.).

Put this in your styles:

<style name="MyTheme" parent="AppTheme.NoActionBar">
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:backgroundDimEnabled">false</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowActionBar">false</item>
   <item name="android:windowFullscreen">false</item>
   <item name="android:windowContentOverlay">@null</item>
</style>

Then add MyTheme to your activity manifest: <activity android:name=".MyActivity" android:theme="@style/MyTheme">

nntona
  • 409
  • 4
  • 9
Vadim Leb
  • 731
  • 7
  • 6
4

First have the Transparent Theme activity:

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

Assign the transparent theme to your activity in Manifest:

    <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:theme="@style/Theme.Transparent"/>

Create the layout as per your requirement and set the content view of your activity to that layout.

Try following layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_alignParentTop="true">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>

Hope it is helpful.

Veer
  • 2,071
  • 19
  • 24
  • It doesn't work. I get my Menu button in the middle (like image number 3) without the translucent effect. – Xavi Gil May 07 '12 at 14:05
  • can you post your layout xml which have menu button? – Veer May 07 '12 at 14:09
  • Try layout which is added in my answer – Veer May 07 '12 at 14:22
  • I still get image number 3 without the translucent effect. To be more precise, I see the background red, and the activity 2 is in the center of the screen. Have you tried your proposal? Does it work for you? – Xavi Gil May 07 '12 at 14:27
  • I didn't tried myself, but i used transparent theme in one of my project. But, it was having dialog. Can you please try giving android defined translucent theme android:theme="@android:style/Theme.Translucent" in Manifest? – Veer May 07 '12 at 14:38
0

I am not sure how to do it through the xml, but this should work programmatically, try adding this to your activity (maybe to the mainview of the activity).

//grab layout params
WindowManager.LayoutParams lp = this.getWindow().getAttributes();

//remove the dim effect
lp.dimAmount = 0;
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • I tried it without Theme applied to my activity, and I got the same result as in image number 2. Applied with my CustomTheme, I got number 3 without the translucent effect. – Xavi Gil May 07 '12 at 14:11
  • @Xavi hmmm, well maybe the dim isn't from the activity but the main layout or something. What about trying calling getWindow() on that? Maybe try adding an id to the realtivelayout then grad the layout by findViewBy and then call get window on the layout. Any change? – MikeIsrael May 08 '12 at 05:00