Android Take Scrollview Screenshot Tutorial with Example is your today’s guide.
When the screen size is limited but information on the single screen in large, we will simply use scrollview. But when you want to take the screenshot of the whole screen, some problems arise.
It is easy to take the screenshot of current screen but it is little tough when the screen have the scrollview with it.
With the simple and usual techniques, system will not capture the whole scrollview.
In this example, we will capture the whole scrollview programmatically even if it is double or more longer than the screen of the android device.
Normally, the pages having information about the person or school or organisation have more height than the average android device screen size. You must use scrollview in this scenarios.
In these cases, this tutorial will help you a lot and will save your time and efforts.
Output Video
After completion of all above code stuff, you should get output like following video
1. All Required Permissions
In this tutorial, you need to ask for required permissions.
Below are these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I have set the target sdk version as 22, so no need for runtime permission here.
2. Setting up the scrollview
Update your activity_main.xml file with below source code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="Take Screenshot of below scrollview!" android:textSize="20sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scroll"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll" android:layout_marginTop="10dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:gravity="center" android:text="First Text!!" android:textSize="30sp" android:textColor="#fff" android:background="#205dd7"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginTop="10dp" android:src="@drawable/benz" android:scaleType="fitXY"/> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginTop="10dp" android:gravity="center" android:text="Second Text!!" android:textSize="30sp" android:textColor="#fff" android:background="#de0f39"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginTop="10dp" android:src="@drawable/bike" android:scaleType="fitXY"/> <TextView android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginTop="10dp" android:gravity="center" android:text="Third Text!!" android:textSize="30sp" android:textColor="#fff" android:background="#09e936"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:layout_marginTop="10dp" android:src="@drawable/silver" android:scaleType="fitXY"/> </LinearLayout> </ScrollView> </LinearLayout>
I have taken scrollview with six child views in it.
On the button click, system will capture the screenshot of the scrollview.
I have set three different images in above code. Download them by clicking below link.
[sociallocker]Download Required Images[/sociallocker]
Add following code into the MainActivity.java file
import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ScrollView; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; public class MainActivity extends AppCompatActivity { private ScrollView scrollView; private Button btn; public static Bitmap bitScroll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = findViewById(R.id.scroll); btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bitScroll = getBitmapFromView(scrollView, scrollView.getChildAt(0).getHeight(), scrollView.getChildAt(0).getWidth()); saveBitmap(bitScroll); Intent intent = new Intent(MainActivity.this,PreviewActivity.class); startActivity(intent); } }); } //create bitmap from the ScrollView private Bitmap getBitmapFromView(View view, int height, int width) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) bgDrawable.draw(canvas); else canvas.drawColor(Color.WHITE); view.draw(canvas); return bitmap; } public void saveBitmap(Bitmap bitmap) { Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpeg"; File imagePath = new File(mPath); FileOutputStream fos; try { fos = new FileOutputStream(imagePath); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); Toast.makeText(getApplicationContext(),imagePath.getAbsolutePath()+"", Toast.LENGTH_LONG).show(); Log.e("ImageSave", "Saveimage"); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } } }
Consider the below code
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bitScroll = getBitmapFromView(scrollView, scrollView.getChildAt(0).getHeight(), scrollView.getChildAt(0).getWidth()); saveBitmap(bitScroll); Intent intent = new Intent(MainActivity.this,PreviewActivity.class); startActivity(intent); } });
This is the code for the button click.
When the user will click the button, system will capture the screenshot of the scrollview.
A method named getBitmapFromView() will be executed here. We need to provide scrollview, it’s height and it’s length in the parameters of the getBitmapFromView() method.
Below is the code for getBitmapFromView() method.
//create bitmap from the ScrollView private Bitmap getBitmapFromView(View view, int height, int width) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) bgDrawable.draw(canvas); else canvas.drawColor(Color.WHITE); view.draw(canvas); return bitmap; }
It will create the bitmap from the view object of the scrollview, scrollview’s height and length.
getBitmapFromView() method will return the bitmap of the scrollview.
This bitmap can be used easily to store the image in the internal storage.
Saving the image
Following method will save the screenshot of the scrollview using the bitmap.
public void saveBitmap(Bitmap bitmap) { Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpeg"; File imagePath = new File(mPath); FileOutputStream fos; try { fos = new FileOutputStream(imagePath); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); Toast.makeText(getApplicationContext(),imagePath.getAbsolutePath()+"", Toast.LENGTH_LONG).show(); Log.e("ImageSave", "Saveimage"); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } }
The bitmap returned by the getBitmapFromView() method will be passed as the parameter of the saveBitmap() method.
After saving the screenshot successfully, compiler will run the below coding lines.
Intent intent = new Intent(MainActivity.this,PreviewActivity.class); startActivity(intent);
This will simply open an activity which will preview the generated screenshot in an imageview.
For this, we need to create a new activity.
3. Previewing the screenshot
Make a new activity and give it a name “PreviewActivity”.
Add below code in activity_preview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".PreviewActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginTop="10dp" android:gravity="center" android:text="Below is your ScreenShot" android:textSize="20sp" android:layout_marginBottom="20dp"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/silver" android:id="@+id/img"/> </LinearLayout> </ScrollView> </LinearLayout>
Write below code in PreviewActivity.java file
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class PreviewActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_preview); imageView = findViewById(R.id.img); imageView.setImageBitmap(MainActivity.bitScroll); } }
There is one imageview in this activity. This image will show us the screenshot of the whole scrollview.
Screenshot is set into the imageview by below line
imageView.setImageBitmap(MainActivity.bitScroll);
I have declared a bitmap called bitScroll as public static in the Main Activity.
So we can use it in any other activity of our app. Here, I have used that public bitScroll variable to set the image in preview activity.
All the words for android take scrollview screenshot tutorial with example are written completely.