HudaOmais
HudaOmais

Reputation: 37

Change Background Image of RelativeLayout from within Java Class (Android App)

So I am working on an Android application using Android 4.0 Library.

One of the activities of this application is made up of a RelativeLayout that has an image background and a toggle button. The background image of the layout must change when the user toggles the button.

So it must be changed from inside the activity.java class:

if (toggleButton.isChecked()){

// Change the background of the activity to image 2 (for example)           
 }

else{ // when toggle button is off

// Change it back to image 1

}

Please help me with this. Thank you :)

Upvotes: 1

Views: 4479

Answers (1)

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

You use the method, setBackground in the View class:

if (toggleButton.isChecked()){

// Change the background of the activity to image 2 (for example) 
View myView =  this.findViewById(yourViewId); 
myView.setBackgroundResource(yourImage);         
 }

else{ // when toggle button is off

// Change it back to image 1
// Change the background of the activity to image 2 (for example) 
 View myView =  this.findViewById(yourViewId); 
myView.setBackgroundResource(yourOtherImage);
}

Upvotes: 1

Related Questions