0

I am working on a simple app and I want to change the background image. Right now it's just a plain color. I want to be able to use an image from the web by changing the URL in the Java Code. So what I need to know is how to change this tag from the XML android:background="#FFFFFF" to some image I found at the following website:

enter image description here

user
  • 86,916
  • 18
  • 197
  • 190
user1174834
  • 239
  • 3
  • 7
  • 14
  • possible duplicate of [android picture in background](http://stackoverflow.com/questions/8058950/android-picture-in-background) or ... http://stackoverflow.com/questions/3307090/how-to-add-background-image-to-activity ... or ... http://stackoverflow.com/questions/7784992/how-to-set-image-in-backgound-display ... or ... – Brian Roach Feb 21 '12 at 21:32
  • Answer to [Change background image of Framelayout via URL](http://stackoverflow.com/questions/8586097/change-background-image-of-framelayout-via-url) question works for this one too. – AaA Aug 29 '12 at 02:00

2 Answers2

4

You can't link an image from the web directly in the xml declaration of a View. You either download the image and then put it in the drawable folder and reference it like this:

android:background="@drawable/name_of_the_image"

or (in java code) you parse that address, obtain the image and then set it as background in code in your activity.

user
  • 86,916
  • 18
  • 197
  • 190
  • Your answer has the suggestion but is not answering question. If it is possible to read image from web using java and setting background, answer will be a code, hint or url for how to do it. – AaA Aug 29 '12 at 01:53
  • @BobSort My answer does answer the question, you can't link directly to an image from an url. Regarding on how to do it there are a lot of tutirlas, resources and stackoverflow question with info on how to do it. All it takes is a simple search. – user Aug 29 '12 at 05:52
  • you are underestimating stackoverflow, this question was on top of my search list and the next 15 results were referring to links mentioned by Brain (comment in question) as possible duplicates which is not. Asking to search does not add value to your answer, it only increases time to find the material I need. Btw, you are not the only one who knows about searching, everyone has a knowledge of it in some way. – AaA Aug 29 '12 at 06:00
  • @BobSort The second link after searching `android get image from url`(in google) : http://stackoverflow.com/questions/6407324/how-to-get-image-from-url-in-android , does this answer the question? – user Aug 29 '12 at 07:21
0

I think you need the View.setBackgroundDrawable(Drawable d) method.

Silver
  • 12,437
  • 2
  • 14
  • 11