1

OnBind of recyclervIEW:-

@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position)
{
  holder. tv_bill_no.setText(context.getString(R.string.bill_no_display , 
    String.valueOf(12))  );
    holder.tv_total_amt_label.setBackground(context.getResources().getDrawable(R.drawable.textline));
    holder.lv_product_sub_totals.setEmptyView(holder.emptyview);
    Typeface typeface=Typeface.createFromAsset(context.getAssets(),"Capture_it.ttf");
    holder.tv_dist_name.setTypeface(typeface);
    holder.tv_generate.setTypeface(typeface);
    holder.btn_generate_pdf.setVisibility(View.GONE);
   // itemView.setVisibility(View.GONE);
    holder.btn_generate.setVisibility(View.GONE);
    holder.btn_share.setVisibility(View.GONE);

    holder.ll.setDrawingCacheEnabled(true);
    // ImageView iv = (ImageView) rootview.findViewById(R.id.bdf_iv_bill);

    Bitmap bm = Utility.screenShot(holder.ll);
    bitmap_pdf_pages.add(bm);

    Log.e("width",""+holder.ll.getWidth());

   }

Its throwing an error java.lang.IllegalArgumentException: width and height must be > 0

What I am doing is in OnBind i am taking a screenshot of each view and adding it ta ArrayList<Bitmap> but I am unable to do so. I want a solution where I can take the screenshots of views of a listview separately not in a single go. Others opinions are accepted.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

1 Answers1

1

It is throwing an error because View.getHeight() (I assume it is being used in Utility.screenShot(holder.ll)) only has value after the view has been measured and at OnBindViewHolder that has not happened yet.

So you have to force the measure manually and use view.getMeasuredHeight() instead, taking the same constraints as the ones defined in the axml before you "take the screenshot".

For the example I'm taking a width and height measurement of 300.

View u = holder.ll;
holder.ll.measure(View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.EXACTLY));
u.setDrawingCacheEnabled(true);
int totalHeight = holder.ll.getMeasuredHeight();
int totalWidth = holder.ll.getMeasuredWidth();
u.layout(0, 0, totalWidth, totalHeight);    
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
u.setDrawingCacheEnabled(false);

Sources:

HIH

fmaccaroni
  • 3,846
  • 1
  • 20
  • 35
  • I got ur point , i will try it giving a fixed height and width(though it might be an issue because not all `views` are of `equal dimemsion`) and give you the feedback tomorrow.. :) – Santanu Sur Jan 03 '18 at 15:49
  • It's not about fixed height or not, it is about measuring before you take the screenshot. You can set "wrap_content" using View.MeasureSpec.AT_MOST for example. Follow the link in the source and there it is explained or check https://developer.android.com/reference/android/view/View.MeasureSpec.html. Hope it helps :D – fmaccaroni Jan 03 '18 at 16:26
  • For the example I'm taking a width and height measurement of `300` . What is actually 300? – Santanu Sur Jan 04 '18 at 07:19
  • AFAIK it is in pixels so if you want dp just do the math with the device display density like https://stackoverflow.com/questions/4605527/converting-pixels-to-dp – fmaccaroni Jan 04 '18 at 17:58