I need to take screenshot programmatically of activity view. I have found a lot of answers how to do it, but there is not answer how to do it with opened dialog
Asked
Active
Viewed 4,776 times
12
-
1did you look at [this](http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android/5651242#5651242) and all the other responses here and comments too. – Rat-a-tat-a-tat Ratatouille Feb 04 '14 at 08:12
-
This looks like a duplicate question: [How to capture everything currently on screen including Dialogs](http://stackoverflow.com/questions/16089225/how-to-capture-everything-currently-on-screen-including-dialogs). – jww Feb 06 '14 at 07:45
4 Answers
8
Ok this one is a little tricky. There is no direct way to do it as far as I know. Here is something that works. I tried to take individual shots and layer them.
Dialog dialog = // Dialog that is showing
View mainView = getSupportActivity().getWindow().getDecorView();
mainView = getSupportActivity().getWindow().getDecorView()
.findViewById(android.R.id.content);
mainView.setDrawingCacheEnabled(true);
// This is the bitmap for the main activity
Bitmap bitmap = mainView.getDrawingCache();
View dialogView = dialog.getView();
int location[] = new int[2];
mainView.getLocationOnScreen(location);
int location2[] = new int[2];
dialogView.getLocationOnScreen(location2);
dialogView.setDrawingCacheEnabled(true);
// This is the bitmap for the dialog view
Bitmap bitmap2 = dialogView.getDrawingCache();
Canvas canvas = new Canvas(bitmap);
// Need to draw the dialogView into the right position
canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1],
new Paint());
String filename = // filename to save
File myPath = new File(filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Trace.d("Twitter", "The file path is " + myPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I just tried this and it worked. Hope this helps. Let me know if it doesn't work.

Aswin Rajendiran
- 3,399
- 1
- 21
- 18
-
-
Yes in this example, it is a custom dialog. I will look into the AlertDialog when I get some time later today. – Aswin Rajendiran Feb 04 '14 at 22:21
-
Add this code while merging the bitmaps to create a darker background(this will give a real dialog pop-up effect) : .. //Create a transparent dark layer to add to background Paint p = new Paint(); ColorFilter filter = new LightingColorFilter(0xFF999999, 0x00000000); p.setColorFilter(filter); // Canvas canvas = new Canvas(bitmap); //Add dark layer to background canvas.drawBitmap(bitmap,location[0],location[1],p); // //draw dialog over background canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1], new Paint()); .. – Swas_99 Nov 03 '15 at 21:56
1
I have code for capture screen programmatically..
hope this code help you..
public class CaptureScreenShots extends Activity {
LinearLayout L1;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_shots);
L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
Button but = (Button) findViewById(R.id.munchscreen);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.screen_shots, menu);
return true;
}
}
below are layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/munch"
android:id="@+id/munchscreen"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenshots"
android:contentDescription="@string/app_name"
/>
</LinearLayout>

Bhavesh Vadalia
- 805
- 6
- 23
0
Use the "screen capture" button in the "Devices" window of DDMS with a device/emulator connected. This allows you to take screenshots of whatever is on screen, including when a dialog is open.
EDIT: when I answered this question, it was not clear it needed to be done programmatically and was later edited to clarify.

Ken Wolf
- 23,133
- 6
- 63
- 84
-
1
-
1I did not know that! Maybe checkout Android Screenshot Library if you haven't already: http://stackoverflow.com/a/16749004/833647 – Ken Wolf Jun 30 '13 at 18:54
0
This is work for me (but without dialog):
public class MainActivity extends AppCompatActivity {
private View main;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here we Checking CALL_PHONE permission
setContentView(R.layout.activity_main);
main = findViewById(R.id.main);
imageView = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Bitmap b = Screenshot.takescreenshotOfRoot(imageView);
imageView.setImageBitmap(b);
main.setBackgroundColor(Color.parseColor("#999999"));
}
});
}
}
Screenshot class:
public class Screenshot {
public static Bitmap takescreenshot (View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takescreenshotOfRoot(View v) {
return takescreenshot(v.getRootView());
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.balance.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Take Screenshot"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.055" />
<ImageView
android:id="@+id/imageView"
android:layout_width="355dp"
android:layout_height="485dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/menuitem_background"
tools:layout_editor_absoluteX="17dp" />
</android.support.constraint.ConstraintLayout>

Majid ff
- 249
- 7
- 22