Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Text on ImageView in android coding?

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?

like image 770
GOBINATH.M Avatar asked Jul 23 '13 12:07

GOBINATH.M


2 Answers

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");
like image 50
Mena Avatar answered Oct 05 '22 01:10

Mena


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.

like image 29
Skaard-Solo Avatar answered Oct 05 '22 00:10

Skaard-Solo