Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add rounded corners to Translucent background of activity?

I have a simple Activity and I would like to have the a rounded rectangle shape. The activity uses a translucent Drawable. I have seen popup windows by other developers that are translucent (not a dialog theme) with rounded corners and I am trying to replicate that. Any help would be appreciated.

Here is the code I currently have that produces a rectangular translucent window in the middle of the screen.

<style name="Theme.TranslucentDialog">
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <!-- Note that we use the base animation style here (that is no
         animations) because we really have no idea how this kind of
         activity will be used. -->
    <item name="android:windowBackground">@drawable/translucent_background</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorForeground">#fff</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> 
</style>

<drawable name="translucent_background">#60000000</drawable>
like image 585
Camille Sévigny Avatar asked May 23 '11 18:05

Camille Sévigny


1 Answers

I was not able to do this by generating a custom shape in the code. I had to accomplish this by creating my own 9-Patch png file. Here is the final Theme that I created:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
    </style>

    <style name="Theme.TranslucentDialog">
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorForeground">#ffffff</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> 
        <item name="android:windowBackground">@drawable/notification_panel</item>
    </style>

</resources>

Please note the line:

<item name="android:windowBackground">@drawable/notification_panel</item>

This is the line that sets the background of the activity to the 9-Patch image that I created. This image could be anything, an image, a 9-patch or a custom shape. I used a 9-Patch so that I could have a nice border and rounded corners while retaining a very translucent activity window (showing everything behind it but leaving a nice hue of grey where the activity window is located).

The 9-patch notification_panel.9.png is located in my drawable folder. At first I was a little bit intimidated to create my own 9-patch image, but it turns out that by using Inkscape and the android draw9patch.bat utility program, I was able to do this with satisfactory results.

Let me know if anyone has any questions.

like image 170
Camille Sévigny Avatar answered Sep 28 '22 02:09

Camille Sévigny