Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Programmatically Take a Screenshot on Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Ever wanted to take some perfect screenshot of a particular view, or perhaps some UI element ruined your favorite screenshot? Don’t worry much, this Geeks for Geeks article will help you achieve it by making an app from scratch. As Below is the code for the title name in this article, we are going to discuss how to take Screenshots programmatically on Android.

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 Kotlin as the programming language.

Step 2: Designing the layout file

We will commence by starting designing the layout which you want to capture, here we’ll add only a basic text view and then take the screenshot. 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 version="1.0" encoding="utf-8"?>
            android:text="I am screenshot"

Step 3: Making the Code work

We will now add the code which is the main crux of the topic, this will make actually take the screenshot and then store it on your device.


private fun takeScreenShot(view: View): Bitmap {
    val thescreenshot = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
    val draw = Canvas(thescreenshot)
    val drawGeeks = view.background
    if (drawGeeks != null) drawGeeks.draw(draw)
    else draw.drawColor(Color.BLACK)
    return thescreenshot

View refers to the layout view that we wish to capture a snapshot of. We have a view with id as a container in our code.

Step 4: Doing even better things

We can also add the below methods to the file so that we can achieve some additional functionality and then take the screenshot again so that it is better. In this case, the view will be the id of ConstraintLayout (i.e. container). In this method, we will first construct an empty bitmap, which we must return as the function’s value. Then we build a Canvas and use the bitmap to draw on it. It takes the view’s backdrop in bgDrawable. And now, we’ll use view.draw to draw the view on canvas (canvas). Finally, we return the bitmap we created, which is the Bitmap of the view. Now, in the Activity file, call the aforementioned method as follows:


class GeeksforGeeksActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val geeksforgeekslLayout: ConstraintLayout = findViewById(
    private fun takeScreenShot(view: View): Bitmap {
        val screenshot= Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
        val drawBoard = Canvas(returnedBitmap)
        val actualScreen = view.background
        if (actualScreen != null) actualScreen.draw(drawBoard)
        else drawBoard.drawColor(Color.WHITE)
        return returnedBitmap


Image #1. The output.

Now, this is the view which we created in Step #1, this is perfect for situations when we need to conclude or isolate some special information from hay of info and also seclude the information.


This method may be used to produce bitmaps for whatever view you desire, as well as screenshots of any widget, such as ImageView or TextView.

My Personal Notes arrow_drop_up
Last Updated : 27 Sep, 2021
Like Article
Save Article
Similar Reads
Related Tutorials