alejaandroe
alejaandroe

Reputation: 53

Change the background picture in Android

I want to change the picture of the background in my app in Android Studio depending on a few conditions. This is my code:

switch (sky_state) {
    case "SUNNY":
        // Here I want to change the background
        break;
}

I know how to change it in the XML file of the layout, but is it possible to access this property from the code outside the layout file?

Upvotes: 2

Views: 1754

Answers (2)

ashwini
ashwini

Reputation: 41

view.setBackground(getResources().getDrawable(R.drawable.icsend));

Upvotes: 1

Rohit Jagtap
Rohit Jagtap

Reputation: 1670

Yes, you can do that... Use the following line of code for that particular view...

        switch (sky_state) {
        case "SUNNY":
            view.setBackground(getResources().getDrawable(R.drawable.ic_send));
            break;
        }

Let me know if this works for you and mark as an answer if it does, so that it would be useful to others.....

Upvotes: 1

Related Questions