Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaHow to Set a Transparent Background of an ImageView on Android?

How to Set a Transparent Background of an ImageView on Android?

Android applications use different types of images for displaying within our Image View. Many times we are using an image view which is having a specific background and we have to set the background for that image to be transparent. So that the image will better match the background color of the android application. In this article, we will take a look at How to Set a Transparent Background for our Image View for our android application.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Adding images to your drawable folder

Copy the image which you want to add and inside the android studio project. Navigate to app>res>drawable. Right-click on it and simply paste your image into the drawable folder. 

Step 3: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail.  

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idIVLogo"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="ImageView with Transparent Background in Android"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--Image View for adding a transparent background-->
    <ImageView
        android:id="@+id/idIVLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:src="@drawable/gfglogo" />
  
</RelativeLayout>


Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on the below line we are creating a variable.
    lateinit var logoIV: ImageView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing our variables.
        logoIV = findViewById(R.id.idIVLogo)
  
        // on below line we are setting background color to transparent
        logoIV.setBackgroundColor(resources.getColor(android.R.color.transparent))
  
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on the below line we are creating variables.
    private ImageView logoIV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing our variables.
        logoIV = findViewById(R.id.idIVLogo);
  
        // on below line we are setting background color to transparent
        logoIV.setBackgroundColor(getResources().getColor(android.R.color.transparent));
  
    }
}


Now run your application to see the output of it. 

Output:

Output

RELATED ARTICLES

Most Popular

Recent Comments