Rahul Rahul - 1 year ago 92
Android Question

How to make textview with drawable on the left which could be set programmatically from a url coming from server

Textview with programmable drawable on the left

How to make textview with drawable on the left that(drawable) can be set programmaticaly from a url coming from the server.

Answer Source

Well, the first step is to decode the image from the URL.

You could do something like this:

String yourUrl = "http://someUrl"; // insert your URL here

// connect, get an instance of the InputStream
HttpURLConnection connection = (HttpURLConnection) new URL(yourUrl).openConnection();
InputStream inputStream = connection.getInputStream();

// decode the stream into a Bitmap and create a Drawable from it
Bitmap tempBitmap = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(getResources(), tempBitmap);

And then, set it as a compound drawable on the left side of your TextView:

// the order is left, top, right, bottom, so you need to set the first param
yourTextView.setCompoundDrawables(drawable, null, null, null);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download