Search code examples
androidbitmapandroid-linearlayout

Save Layout as Bitmap


I want to save my Linearlayout as an bmp.

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout_QrCode" >

        <Space
            android:layout_width="15dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

            <ImageView
                android:layout_width="185dp"
                android:layout_height="185dp"
                android:id="@+id/imageView_qrcode" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="417dp"
            android:layout_height="match_parent" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true"
                android:id="@+id/space2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_label"
                android:layout_alignParentStart="true"
                android:layout_above="@+id/space2"
                android:textStyle="bold"
                android:textSize="40sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_serial"
                android:layout_below="@+id/space2"
                android:layout_alignParentStart="true"
                android:textSize="30sp" />
        </RelativeLayout>

    </LinearLayout>

Therefore I use the following code to perform this:

        public Bitmap viewToBitmap(View view) {
            Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            return bitmap;
        }

But If I run this code my bitmap only contains the ImageView (ImageView_qrcode). I checked it. If I inflate the Linearview the TextViews are shown correctly but the output bmp only contains the ImageView does somebody know why?


Solution

  • Thanks to @Gil Moshayof it works now!

    After calling the getBitmapFromView method via post it works fine

                    qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
                    labelSticker.setText(qrDataList[1]);
                    serialSticker.setText(qrDataList[2]);
    
    
                    qrcodefinal.post(new Runnable() {
                        @Override
                        public void run() {
                            qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
                        }
                    });