2

In my application i have to show an image in my text view. Also When i click 1st time 1 image should draw and clicking next time another image should be drawn.Is it possible to draw an image in textview?Please help me..

4
  • 1
    and why do u want to do that!!, You have image views to show images
    – AAnkit
    Jul 23, 2012 at 9:37
  • To display image in textview stackoverflow.com/questions/3699583/… Jul 23, 2012 at 9:45
  • 1
    Tell us why you need it with a TextView, so that if there are alternatives available they will be provided. Jul 23, 2012 at 9:47
  • Actually b4 i displayed one string in that textview..So my thinking gone that way itself (i.e) whether is it possible to have a image in case of text..thats y...
    – prakash .k
    Jul 23, 2012 at 10:18

5 Answers 5

3

You can use

setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

for the textView

1
  • You add compound drawables through the xml as weel: ` android:drawableStart="@drawable/ic_close" ` for example
    – pantos27
    Mar 16, 2017 at 10:25
2

You can set a background image using the android:background attribute

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/image" />

Or programatically with the setBackgroundDrawable()or setBackgroundResource() methods:

textView.setBackgroundResource(R.drawable.image);
3
  • HI fiddler..Thanks its working..One more fiddler.In my app when oi clicking textview 2 automatically textview 1 image should disappear. is it possible?????
    – prakash .k
    Jul 23, 2012 at 10:22
  • Sorry but I don't understand your question...could you explain a bit?
    – sdabet
    Jul 23, 2012 at 11:40
  • You can assign this to the Textview: android:clickable="true" to register touch events and add this too: android:onClick="textViewOnClick" Then, you need to implement the method textViewOnClick on your activity like this: public void textViewOnClick(View v){ //logic to change background image on every click with //textView.setBackgroundResource(R.drawable.image); }
    – Chronos
    Jul 23, 2012 at 13:11
1

You can take an array of Drawables, an an index for that:

TextView tv;
Drawable[] drbl = new Drawable[4];
int drblIndex = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    drbl[0] = getResources().getDrawable(R.drawable.ic_launcher);
    drbl[1] = getResources().getDrawable(R.drawable.img2);
    drbl[2] = getResources().getDrawable(R.drawable.img3);
    drbl[3] = getResources().getDrawable(R.drawable.right_arrow);
    tv = (TextView) findViewById(R.id.textView1);
    tv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tv.setBackgroundDrawable(drbl[drblIndex++]);
            if(drblIndex == drbl.length)
                drblIndex = 0;
        }
    });
}

Then you can set values of that array as I have done.

Then onClick you can move to next Index and can set new Drawable-Image to TextView.

When Index reaches to last value, set it to zero, simply.

1
  • Oops!!Ok will delete mine One.Dn't think that i have duplicated..our logic matched..i m happy that i am thinking like other humans..::)))) Jul 23, 2012 at 9:58
0

Set OnClickListener on the TextView and in its onClick() method, use the following methods to set the images :

textview.setBackgroundDrawable();

or

textview.setBackgroundResource();
0

try this code, textview with image.

CharSequence dogstate = null ; Html.fromHtml("" + " " + "your text" + "
", featuregetter, null);

dogatstatus.append(dogstate);

private ImageGetter featuregetter = new ImageGetter() {

    public Drawable getDrawable(String source) {

        Log.i(" get drawable mathod ", "");
        Bitmap B = BitmapFactory.decodeResource(getResources(),
                R.drawable.bone);
        BitmapDrawable BD = new BitmapDrawable(B);

        BD.setBounds(0, 0, B.getWidth(), B.getHeight());

        return BD;
    }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.