0

I am unable to get bitmap of expandable listview This is what I tried

private Bitmap convertViewToBitMap() {
    View printlayout = (View) getActivity().findViewById(R.id.expandableList);

    printlayout.setDrawingCacheEnabled(true);


    printlayout.measure(
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    printlayout.layout(0, 0, printlayout.getMeasuredWidth(),
            printlayout.getMeasuredHeight());

    printlayout.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(printlayout.getDrawingCache());
    printlayout.setDrawingCacheEnabled(false); // clear drawing cache
    return b;
}
Tushar Thakur
  • 956
  • 3
  • 15
  • 32

3 Answers3

1

Following method helped me

private Bitmap convertViewToBitMap() {
View printlayout = (View) getActivity().findViewById(R.id.expandableList);
    printlayout.setDrawingCacheEnabled(true);
    Bitmap b = printlayout.getDrawingCache();
    return b;

}

Tushar Thakur
  • 956
  • 3
  • 15
  • 32
0

I think all you need it to get the hold of the view and convert that view into the bitmap and then print the bitmap.

  1. You can get the view using findViewById();
  2. For converting view into bitmap, a quick google may help more, but i found this
  3. And once you have bitmap, do what ever you want.

@Pietu1998 pointed correctly, also show your attempt so reduce chance for downvoting.

Community
  • 1
  • 1
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

You can re-draw your list into a bitmap which you can then use.

Here I'm issuing a re-draw on an instance variable mListView and then using that bitmap for my ImageView. Note that I'm using a Handler to force the re-draw on the UI thread.

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap = Bitmap.createBitmap(mListView.getWidth(),
                    mListView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            mListView.draw(canvas);
            // Do something the 
            ((ImageView)mContext.findViewById(R.id.img)).setImageBitmap(bitmap);
        }
    }, 1);

Also note that you will need to have the ImageView inside of your xml already and the ListView not taking up all the space in the layout.

Simas
  • 43,548
  • 10
  • 88
  • 116