user1174834
user1174834

Reputation: 249

Change Background In Java Code

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

Upvotes: 0

Views: 679

Answers (2)

Silver
Silver

Reputation: 12475

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

Upvotes: 0

user
user

Reputation: 87074

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.

Upvotes: 4

Related Questions