How to write Text on ImageView in android coding?
Asked Answered
C

4

12

Hi I've been trying to write Numbers on Imageview. Images for N number of questions. If the user entered answer is correct, then it should be displayed with question number with tick mark image else question number with wrong mark image. I need to write the question number on the image. My code is here:

    LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_view_report);
    LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
    ImageView[] imgview=new ImageView[questions.length];
    for(int i=0;i<no_of_questions;i++)
    {
                if(userEnteredAnswers[i]==correct_answer[i]){           
                    Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.correct);
                    Canvas canvas = new Canvas(bm);
                    Paint paint = new Paint(); 
                    paint.setColor(Color.BLACK); 
                    paint.setTextSize(10); 
                    canvas.drawText(i, 5, 5, paint);

                    imgview[i]=new ImageView(this);
                    imgview[i].setImageDrawable(new BitmapDrawable(bm));
                    l_layout.addView(imgview[i]);
                }
                else {          
                    Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.wrong);
                    Canvas canvas = new Canvas(bm);
                    Paint paint = new Paint(); 
                    paint.setColor(Color.BLACK); 
                    paint.setTextSize(10); 
                    canvas.drawText(i, 5, 5, paint);

                    imgview[i]=new ImageView(this);
                    imgview[i].setImageDrawable(new BitmapDrawable(bm));
                    l_layout.addView(imgview[i]);
                }       
            }

I get this warning:

The constructor BitmapDrawable(Bitmap) is deprecated

Image doesn't showing at run time. What am I doing wrong?

Crowbar answered 23/7, 2013 at 12:8 Comment(0)
G
19

I believe the easiest workaround without overriding anything would be to have a TextView and set its background with a drawable resource.

For instance:

TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
Granjon answered 23/7, 2013 at 12:21 Comment(4)
Hey thank you for ur idea. It's working!!! But I want the text to be in a particular position of the image. How to do that?Crowbar
@VigneshBala I think it would be very difficult if ever possible to reference specific parts of the drawable. Of course, you can move the text around with the gravity attribute...Granjon
This is the easiest solution and requires the least code.Spindling
Only a genius can come up with simple solutions!!Worrywart
D
4

From the Android documentation :

BitmapDrawable(Bitmap bitmap) This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.

You can also create your own view (see Android: Creating Custom Views tutorial for an example) and put you image and the text in this view.

Duvall answered 23/7, 2013 at 12:12 Comment(0)
S
3

1). Make your own layout Say Image_TextView.xml like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imgview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:scaleType="center"
            android:src="@drawable/box" />

        <RelativeLayout
            android:layout_width="150dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="0dp"
            android:background="#80666666" >

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="Hello" 
                android:textColor="#FFFFFF"
                android:textStyle="bold"/>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

This will make Image with Text over it.

2). Then use layout inflator to inflate this view in your parent view.

Note : Using layout inflator you can add other view to your parent view.

Selden answered 23/7, 2013 at 12:23 Comment(0)
C
2

its not possible to write text on imageview, but you can extend it to do so. Alternatively,

  1. you can add an element overlapping the ImageView in the layout, and make it visible only when your condition becomes true.

  2. or, you can use textView to display image, as a background or as a drawable to left/right..

Ps: however, the option 1 is easier to implement, but it will have additional rendering. so consider the other option first.

Cosetta answered 23/7, 2013 at 12:10 Comment(3)
You can subclass ImageView, override onDraw and call canvas.drawText after super.onDraw. So actually it IS possible to write text on an ImageView.Zashin
@Zashin ... i didn't consider that, as i have suggested other ways which are bit easier.Cosetta
Fair enough. Your second suggestion should be just as good, but the first one would make a redundant draw call, even though it's the easiest way to go about this.Zashin

© 2022 - 2024 — McMap. All rights reserved.