0

I want to create an image out of some text. For example, in this photo I have shown here, I want to make it so that I have an image or some sort of text view that shows a plus sign and then I would programmatically set the number in my code in that image. Here is an example:

enter image description here

As you can see here. I woudl want the exact behavior: to set the +16 as an image or some sort of text view I can programmatically set. I some XML here so that I can create an image based on a URI I pass in here:

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/commenter_photo_four"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="60dp"
            android:layout_marginEnd="60dp"
            android:layout_toLeftOf="@id/num_comments"
            android:layout_toStartOf="@id/num_comments"
            fresco:placeholderImage="@mipmap/blank_prof_pic"
            fresco:placeholderImageScaleType="centerCrop"
            fresco:roundBottomLeft="false"
            fresco:roundBottomRight="false"
            fresco:roundedCornerRadius="5dp"
            fresco:roundingBorderColor="@color/material_color_grey_100"
            android:layout_marginBottom="8dp"
            />

which displays the photo correctly for me. However, I can't seem to be able to insert something like android:text or do some sort of setText method when using the Fresco library (library I use to render my images based on a URI). Then I tried using an ImageView like so:

        <ImageView
            android:id="@+id/commenter_photo_four"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="60dp"
            android:layout_marginEnd="60dp"
            android:layout_toLeftOf="@id/num_comments"
            android:layout_toStartOf="@id/num_comments"
            />

but then I realize that this also doesn't have an android:text capability I can use to set the image to being a + followed by any number I would want to set. Is there any way I can accomplish this +16? I am also thinking I can try a TextView with a circular background possibly?

user1871869
  • 3,317
  • 13
  • 56
  • 106

2 Answers2

0

You can draw text to your imageView by code

Bitmap b=Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
c.drawText("+16",10,10,p);

ImageView i = (ImageView) findViewById(R.id.commenter_photo_four);
i.setImageBitmap(b);
madr
  • 1
  • This works well, thanks. However, I want to set the `ImageView` background. I did something like this: `imageView.setBackgroundColor(ContextCompat.getColor(context, R.color.material_color_grey_200));` but the background looks to be it is a square after i do the `findViewById`. Is there any way to fix this? – user1871869 Sep 14 '16 at 06:22
  • If you want round background - draw by canvas.drawCircle(x, y, radius, paint); as grey circle. Default background in bitmap is transparent. If you set background to some color then if the bitmap is square then the background will be also square. – madr Sep 16 '16 at 15:00
0

you can set imageview and textview in Relative layout and set textview's visibility gone. Then when you want to put text in it set textview's visibility visible.

VNS
  • 44
  • 8