Converting a view to Bitmap without displaying it in Android?


Introduction

In android applications many times we have to get a bitmap from a view and use that bitmap within our android application. The bitmaps are generally created by passing the views to it. In this article we will take a look at How to convert a view to bitmap without displaying it in Android.

Implementation

We will be creating a simple application in which we will be displaying a text view, a button and an image view. Inside the text view we will be displaying the heading of our application. After that we will be adding a button, on click of this button we will be generating our bitmap from a view. Inside the image view we will be displaying the generated bitmap. We will be generating the bitmap from our text view.

Step 1: Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3: Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="400dp"
      android:layout_height="100dp"
      android:layout_centerHorizontal="true"
      android:layout_marginStart="10dp"
      android:layout_marginTop="50dp"
      android:layout_marginEnd="10dp"
      android:gravity="center"
      android:padding="4dp"
      android:text="Converting a view to Bitmap without displaying it in Android"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

   <!-- on below line creating a button to display a bitmap -->
   <Button
      android:id="@+id/idBtnLoadBitmap"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVHeading"
      android:layout_marginStart="10dp"
      android:layout_marginTop="10dp"
      android:layout_marginEnd="10dp"
      android:text="Load Bitmap from Image"
      android:textAllCaps="false" />

   <!-- on below line creating a button to check wifi status-->
   <ImageView
      android:id="@+id/idIVImage"
      android:layout_width="400dp"
      android:layout_height="400dp"
      android:layout_below="@id/idBtnLoadBitmap"
      android:layout_centerHorizontal="true"
      android:layout_margin="10dp" />

</RelativeLayout>

Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating a simple text view in which we are displaying the heading of our application. After this text view we are creating a button which we will be using to generate a bitmap from our heading text view. After this button we are creating an image view. Inside this image view we will be displaying the bitmap which we are generating from our text view.

Step 4: Working with MainActivity.java fil

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // on below line displaying toast message
      Toast.makeText(this, "On Create Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onStart() {
      super.onStart();
      // displaying toast message on below line.
      Toast.makeText(this, "On Start Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onStop() {
      super.onStop();
      // displaying toast message on below line.
      Toast.makeText(this, "On Stop Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onRestart() {
      super.onRestart();
      // displaying toast message on below line.
      Toast.makeText(this, "On Restart Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onResume() {
      super.onResume();
      // displaying toast message on below line.
      Toast.makeText(this, "On Resume Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onPause() {
      super.onPause();
      // displaying toast message on below line.
      Toast.makeText(this, "On Pause Method called.", Toast.LENGTH_SHORT).show();
   }

   // on below line calling on start method
   @Override
   protected void onDestroy() {
      super.onDestroy();
      // displaying toast message on below line.
      Toast.makeText(this, "On Destory Method called.", Toast.LENGTH_SHORT).show();

   }
}

Explanation − In the above code we are creating a variable for our image view, button and a text view. After that we will get to see an onCreate method. This method is used to display the layout file which we have created. Inside this onCreate method we are initializing the variables which we have created. After that we are adding click listeners for the button which we have created. Inside the on click of the button we are calling a method which is used to generate a bitmap from our heading text view. We are generating this bitmap from the loadBitmapFromView method. This method will take a view and then it will generate a bitmap from that view. This generated bitmap we are displaying inside our image view.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

In the above sample application on clicking of the button we are generating a bitmap by passing our text view. We will be generating a bitmap for our heading text view. Now to check whether our bitmap is generated or not. We are setting this bitmap to our image view to display it, so that we can verify that the bitmap has been generated successfully.

Conclusion

In the above article we have taken a look at How to convert a view to bitmap within an android application without displaying it in the android application.

Updated on: 30-Mar-2023

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements