Reputation: 5078
How would it be possible to set a image background to the root view (Relative layout) using volley from a remote server? Usually a drawable can be set to background using following :
RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Resources res = getResources(); //resource handle
Drawable drawable = res.getDrawable(R.drawable.newImage);
rLayout.setBackgroundDrawable(drawable);
But i could not find a way to make drawable out of a image url except a method createFromPath(). I couldn't find how to use volley with it.
Upvotes: 0
Views: 965
Reputation: 1903
You can get the image from the URL like this:
URL url = new URL(imageUrlString);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
And then apply it to your layout using your code like this:
RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout);
Drawable drawable = new BitmapDrawable(getResources(), image)
rLayout.setBackgroundDrawable(drawable);
Please note, it's best to do this in an AsyncTask. The first two lines would go in doInBackground() and the last three lines would go in onPostExecute(). This is how I do it but I don't use Volley, I imagine that would be a lot more complex. If you want to use the Volley image caching then this tutorial is for you.
Upvotes: 1