How to Programmatically Take a Screenshot on Android?
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
<? xml version = "1.0" encoding = "utf-8" ?> < android.support.constraint.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:id = "@+id/container" tools:context = ".GeeksforGeeksActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "I am screenshot" android:id = "@+id/textView" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ android.support.constraint.ConstraintLayout > |
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.
Kotlin
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) view.draw(draw) 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:
Kotlin
class GeeksforGeeksActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) val geeksforgeekslLayout: ConstraintLayout = findViewById(R.id.gfgContainer) takeScreenShot(geeksforgeekslLayout) } 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) view.draw(drawBoard) return returnedBitmap } } |
Output:

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.
Conclusion
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.
Please Login to comment...