Android Take Screenshot Programmatically And Share | Particular Layout

Android take screenshot programmatically and share with whatsapp, gmail, facebook etc. platforms.

In this tutorial, we will also take the screenshot of particular layout.

We will take the screenshot of imageview and textview in this Android take screenshot example. You can also take screenshot of other layout like linearlayout, relativelayout, edittext etc. with the source code of this example.

You will also learn how to save the captured screenshot in the internal storage in the JPEG format.

I will teach you to capture screenshot on button click with android studio.

1. Runtime Permissions

We are going to save the screenshot in the internal storage of the android device.

For this purpose, we need following two permissions.

I have set the target sdk version as 22 but if you want to target greater version than 22, you need to ask for the runtime permissions.

Follow Request Runtime Permissions in android programmatically example for this.

2. Layout And Java Code

In your activity_main.xml file, add following code

I have taken one imageview and three buttons in the above layout file.

Above file will generate the below output

android take screenshot

Basic Layout

Captured screenshot will be previewed in this Imageview (PINK BACKGROUND). One button (TAKE SCREENSHOT) will take the screenshot on it’s click event.

Share button will enable the user to share the screenshot with different apps like whatsapp, gmail, facebook etc.

Now copy and paste below source code into the MainActivity.java file

As you have seen in the above image, Take Screenshot button will capture screenshot with below code

Above method will also save the screenshot in the internal storage.

After saving the image, we will preview it in the imageview.

See the below image which shows the output generated when the screenshot is captured successfully.

android take screenshot

Preview Screenshot

User will click the share button to share the screenshot.

Following method will do this task for us.

I have just called above method on the button click.

Share Intent is used to share the image. Intent will use the URI of the image to share it with various apps.

You will get following screen when this method is executed by the system

android take screenshot

Choose app to share

As you can see that system will allow user to choose from various apps to share the screenshot

If user choose gmail here, then it will open the gmail app and will attach our screenshot

 

android take screenshot

Gmail

Similarly, you can share with other apps like whatspp, facebook, instagram etc.

Android take screenshot tutorial will also guide you to capture specific layout including imageview.

Now, when the user clicks on the third button (SS of particular layout), compiler will open new activity.

3. Screenshot of Particular Layout

Create one new activity and give it a name “ParticularLayoutActivity“.

In your activity_particular_layout.xml file, add following code

In this file, I have taken an Imageview, which will preview the screenshot same as in the previous scenario.

Write the below source code into ParticularLayoutActivity.java file

In this activity we will take the screenshots of two different and particular layouts – Imageview and Textview.

Consider below image

android take screenshot

SS of Particular Layout

As you can see in the above image, we have one image of car and one textview with green background.

There are two buttons above both views.

You can download the image of the car by
[sociallocker]Click here to download car image[/sociallocker]

Following method will take the screenshot of both the views.

This method will also save the screenshot in the internal storage.

Below code will take the screenshot of Car Image.

In the takeSS() method, we need to pass the layout of which we want to capture the screenshot as a parameter.

Here, I have passed the imageview.

Below method will take the screenshot of textview.

Below output is generated when the user takes the screenshot of the car image.

android take screenshot

SS of Car Image

Following image shows the screenshot of the textview.

android take screenshot

SS of textview

So it was all the handy information about android take screenshot tutorial.

Comment out your questions and queries, I will respond it as soon as possible.

Download Source Code For Android Take Screenshot Example

Hit the below link and get your android studio source code.

[sociallocker]Download Source Code For Screenshot Tutorial[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *