0

I need to insert in a TextView a drawable that I download from web in the code. So I can't manually put the image in res and in R.drawable.

How can I use it?

6
  • After downloading the bitmap/jpeg from online, you can change to drawable or u can use as it is. Commented Nov 20, 2015 at 8:07
  • See if stackoverflow.com/questions/12652798/… answers your question. Commented Nov 20, 2015 at 8:08
  • 1
    How is a drawable inserted in a TextView? Commented Nov 20, 2015 at 8:09
  • Drawable can be inserted as a background in a TextView. Commented Nov 20, 2015 at 8:14
  • @VarunKumar yes, you're right Commented Nov 20, 2015 at 8:15

2 Answers 2

2

Steps:

1) Download image

2) Convert to drawable

3) Set drawable to textView by calling smth like this:

textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

try {
    /* Open a new URL and get the InputStream to load data from it. */
    URL aURL = new URL("ur Image URL");
    URLConnection conn = aURL.openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();
    /* Buffered is always good for a performance plus. */
    BufferedInputStream bis = new BufferedInputStream(is);
    /* Decode url-data to a bitmap. */
    Bitmap bm = BitmapFactory.decodeStream(bis);
    bis.close();
    is.close();

    Drawable d =new BitmapDrawable(bm);
    d.setId("1");
    textview.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);// wherever u want the image relative to textview
    } catch (IOException e) {
    Log.e("DEBUGTAG", "Remote Image Exception", e);
    } 

Reference url How to display an image from an URL within textView

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.