How to create a Transparent Activity in Android?
In Android, we can create a transparent activity that will not be visible but your application will be running. The best part of this transparent activity is that you can create a transparent activity by just changing the resource file and we need not write the java or kotlin code for the same.
In this blog, we will learn how to create a transparent activity in Android. So, let’s get started.
Steps to create a Transparent Activity
Step1: Create a project in Android studio with Empty Activity template. In my case, the name of the project is TransparentActivity.
Step2: Open the activity_main.xml file and add the below code:
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="50sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:text="Hello! I am Transparent Activity"
android:textColor="#112090"/>
</RelativeLayout>
Here, we are having one TextView and the text of this TextView will be displayed over the previously opened application.
Step3: Now in the res/values directory, open the styles.xml file and add the below code:
<style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<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>
Let’s look at these property one by one.
android:windowIsTranslucent indicates weather the window in which the activity is present in translucent state or not. It must be a boolean value i.e. it can be true or false.
android:windowBackground is used to set the background of the main window. Here, we are setting the background as transparent.
android:windowContentOverlay is used to overlay over the foreground of the window content area, usually to place a shadow below the title.
android:windowNoTitle is used to indicate whether there should be title on the window or not.
android:windowIsFloating is used to indicate whether the current window is floating or not.
android:backgroundDimEnabled is used to indicate whether the dimming behind the window is enabled or not.
Step4: The final step is to add the theme to your application in the manifest file. So, open the AndroidManifest.xml file and add the below code:
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.Transparent.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Now, run your application and you will see the transparent activity of your application. Great, you are now a magician :)
Hope you enjoyed this blog. Learn more about concepts of Android from our website MindOrks blog .
Keep Learning :)
Team MindOrks!