Signcodeindie
Signcodeindie

Reputation: 796

Android : How to write text to specific area in an image

I am creating an Imagebutton which displays notification number. Here is my sample image:

Icon Image

My goal is to write a number (1,2..) on specific black circle in an image. I tried RelativeLayout and FrameLayout but don't think it is a reliable way to achieve the result. The reason being Margins, Padding will vary on different device sizes.

Any suggestions to get started?

Upvotes: 2

Views: 713

Answers (5)

Joel Fernandes
Joel Fernandes

Reputation: 4856

Here you go:

<FrameLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/pencil">

        <TextView
            android:id="@+id/circle"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:background="@drawable/circle"
            android:gravity="center"
            android:text="2"
            android:layout_gravity="bottom|right"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />

    </FrameLayout>

Images: enter image description here enter image description here

Output:

enter image description here

Upvotes: 2

Hitesh Singh
Hitesh Singh

Reputation: 2005

try this code    
Bitmap bitmap = ... // Load your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(10); 
canvas.drawText("Some Text here", x, y, paint);

Upvotes: 0

Ketan Mehta
Ketan Mehta

Reputation: 272

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom|right"
    android:paddingRight="20dp"
    android:paddingBottom="15dp"
    android:textColor="@android:color/white"
    android:background="@drawable/text"
    android:text="1" />

Upvotes: 0

Vigneshwaran Murugesan
Vigneshwaran Murugesan

Reputation: 757

Try Textview with bacground set as the image

<TextView android:id="@+id/img" android:layout_width="45sp" android:layout_height="45sp" android:background="@drawable/circle" android:gravity="center" android:text="r" /> Use the gravity to align your text inside the image.

Upvotes: 0

Toon Borgers
Toon Borgers

Reputation: 3658

You could make a custom View class which extends from ImageView. You'll have to override the onDraw method and do something like this:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // Calculate x and y positions and set up a Paint instance with correct color/size
    canvas.drawText(String.valueOf(myNumber), x, y, myPaint);
}

Upvotes: 0

Related Questions