I am trying to change the background image of an activity on button click, but not being able to do so. Can you guys tell me how can I can do that?
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
Activity activity = Selection.this;
//Drawable db = Drawable..createFromPath(R.drawable.love1);
// The below line gives error because setbackgroundImage
// is supposed to take a drawable object as an argument.
// Now what to do?
activity.findViewById(android.R.id.content)
.setBackground(Drawable.createFromPath("res/love1.jpg"));
// What to do to correct the above line?
mySong = MediaPlayer.create(Selection.this, R.raw.song1);
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
}
});
I tried with Color.RED using setBackgroundColor but that too is not working. PS: I have not changed anything in the xml file for accomplishing this.
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.LayoutID); mLinearLayout.setBackgroundResource(R.drawable.image_name);
I think the easiest way is to use layout in your
xml
file for your activity.. for example:and then change it from your Activity when you want e.g. after the button click: