Try using this method. The hack here calls the layout and method of measuring the view on its own.
public Bitmap loadBitmapFromView(View v) { v.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); v.draw(c); return bitmap; }
Pass the view in this method, for example
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View inflatedFrame = inflater.inflate(R.layout.my_view, null); Bitmap bitmap = loadBitmapFromView(inflatedFrame.findViewById(R.id.id_of_your_view_in_layout));
source share