How to Create a Transparent Activity in Android?
Last Updated :
05 May, 2021
Many times we have to display a feature in our app where we have to use transparent activity inside our android app. In this article, we will take a look at creating a Transparent Activity in Android.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a simple TextView inside a transparent activity. For creating a transparent activity we will be adding a different style to our activity. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_margin = "4dp"
android:padding = "10dp"
android:text = "Welcome to Geeks for Geeks"
android:textAlignment = "center"
android:textColor = "@color/purple_200"
android:textSize = "25sp"
android:textStyle = "bold"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
</ androidx.constraintlayout.widget.ConstraintLayout >
|
Step 3: Adding a custom style for this activity
Navigate to the app > res > values > themes.xml and add the below code to it.
XML
< style name = "Theme.TransparentActivity" parent = "Theme.MaterialComponents.DayNight.DarkActionBar" >
< item name = "colorPrimary" >@color/purple_500</ item >
< item name = "colorPrimaryVariant" >@color/purple_700</ item >
< item name = "colorOnPrimary" >@color/white</ item >
< item name = "colorSecondary" >@color/teal_200</ item >
< item name = "colorSecondaryVariant" >@color/teal_700</ item >
< item name = "colorOnSecondary" >@color/black</ item >
< item name = "android:statusBarColor" tools:targetApi = "l" >?attr/colorPrimaryVariant</ item >
</ style >
< style name = "Theme.AppCompat.Translucent" parent = "Theme.AppCompat.NoActionBar" >
< item name = "android:background" >@android:color/transparent</ item >
< 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 >
</ resources >
|
Step 4: Changing the theme in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml inside application tag in theme tag add this style in it.
XML
android:theme="@style/Theme.AppCompat.Translucent"
|
Now run your app and see the output of the app.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...