Question

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?

Was it helpful?

Solution

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");

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top