salezica
salezica

Reputation: 76989

Android: snapshot of the root view (with ActionBar)

I used to snap bitmaps of activities by taking their content view and drawing it:

View view = activity.findViewById(android.R.id.content)

Bitmap bitmap = Bitmap.createBitmap(
    view.getWidth(), view.getHeight(), Config.ARGB_8888
);

view.draw(new Canvas(bitmap));

Now I'm using an ActionBar, and it's not nested under the content view, so it's left out. How can I obtain the real root view? Or snap a picture with the action bar in some other way, if that's not possible?

Upvotes: 2

Views: 2152

Answers (1)

Nico
Nico

Reputation: 993

To get a Bitmap for the entire window including ActionBar you can use the DecorView.

  1. First you need to enable drawing cache

    getWindow().getDecorView().setDrawingCacheEnabled(true);
    
  2. get the bitmap

    Bitmap bmp = getWindow().getDecorView().getDrawingCache();  
    
  3. Use the bitmap elsewhere, I try this with a ImageView and works great.

  4. disable drawing cache

    getWindow().getDecorView().setDrawingCacheEnabled(false);
    

Upvotes: 5

Related Questions