0

I'm trying to convert TextView to Drawable to use it as a sticker in my app, but I have a problem, as you see in the picture below, The text becomes cut off from the right, to fix that I added two spaces like that:

When I add only one space like that textview.getText()+ " " I get this :

enter image description here

When I add " " +textview.getText() + " ",text disappears,I see only background :

enter image description here

after I do that, TextView not showing at all when I convert it to Drawable , here's the method to convert TextView to Drawable

 public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        view.draw(canvas);
        return createBitmap;
    }


textView.setText(" " + editText.getText() + " ");
                    Bitmap bitmap = loadBitmapFromView(textView);
                    loadSticker(bitmap);

XML :

               <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center_horizontal|center_vertical"
                android:padding="20dp"
                android:scrollbars="vertical"
                android:shadowColor="#fff"
                android:shadowDx="4"
                android:shadowDy="-4"
                android:shadowRadius="1"
                android:text="Your Text Here"
                android:textColor="#000"
                android:textSize="25sp"
                android:textStyle="bold" />

2 Answers2

0

Edit - Try using non breaking spaces maybe.

textView.setText((" " + editText.getText() + " ").replace(" ","\u00a0"));

Have you tried alternate methods such as,

public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getWidth(), view.getHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getLayoutParams().getWidth(), view.getLayoutParams().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getLayoutParams().getWidth(), view.getLayoutParams().getHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.draw(canvas);
        return createBitmap;
    }
public static Bitmap loadBitmapFromView(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
        return createBitmap;
    }
Chrisvin Jem
  • 3,940
  • 1
  • 8
  • 24
0

Follow this answer to convert any View to Bitmap Convert View to Bitmap

Ahmed Abdalla
  • 2,356
  • 2
  • 14
  • 27