2

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.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Rahul
  • 189
  • 1
  • 13

2 Answers2

2

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);
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
0

Use Volley Class to load images https://developer.android.com/training/volley/request.html

Yash Jain
  • 376
  • 1
  • 3
  • 13