Translucent and Transparent Background for Activity

We are already familiar that an Activity in Android is a single screen on the device but in this article, we will learn how to make it transparent and translucent.

1. Open styles.xml and add the background theme.

<!-- Background Theme for Activity -->
<style name="AppTheme.NoActionBar.TranslucentBackground" parent="AppTheme.NoActionBar">
    <item name="android:windowBackground">@color/colorBackgroundPromotions</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimaryDark">@color/colorBackgroundPromotions</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Set windowIsTranslucent and windowTranslucentStatus tags to true. As the name implies, windowBackground is the background color for an activity and is set to a custom color. Overriding the colorPrimaryDark will make the status bar translucent or transparent. If we want to make the toolbar color translucent or transparent, we can override colorPrimary. Just be careful when doing so, as the text might overlap with the action bar title of the previous activity.

2. To make the activity background translucent, give alpha to any color. Below we have given an alpha of 50 percent to black color.

<color name="colorBackgroundPromotions">#80000000</color>

translucent-activity

Below we have given an alpha of 0 percent to black color. By removing the alpha from any color we can make any color transparent.

<color name="colorBackgroundPromotions">#00000000</color>

tranparent-activity

The column names and the list seem to be placed on the same screen but the UI elements belong to different activities. The list background color is white while the activity that displays the column names is transparent because of zero alpha. Same is the case in the translucent image.

Based on the alpha of the colorBackgroundPromotions, the activity will behave as a translucent or transparent.

Popular posts from this blog

How to Read Metadata from AndriodManifest File

Mr Phone - Find The Next Best Phone You Want To Buy

Add Spacing to Recycler View Linear Layout Manager Using Item Decoration

Add Options Menu to Activity and Fragment

Create Assets Folder, Add Files and Read Data From It