Changing background image on button click android app development

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.


I think the easiest way is to use layout in your xml file for your activity.. for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayoutID" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background_img1" >

and then change it from your Activity when you want e.g. after the button click:

LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayoutID); mLinearLayout.setBackgroundResource(R.drawable.background_img2);


Change <strong>setBackground(Drawable)</strong> to <strong>setBackgroundResource(R.drawable.filename)</strong>


LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.LayoutID); mLinearLayout.setBackgroundResource(R.drawable.image_name);


  • How to load image for Android ImageView from classpath?
  • Download and show the Thumbnail
  • java.io.IOException: Is a directory Android
  • How to set the button image for UIControlStateSelected
  • Appearance proxy affected by upgrade to iOS 6
  • Change UIbutton Image on click
  • IOS Custom UIBarbuttonItem change position in detail view
  • Is it possible to set an Android Notification or a later date and time that will fire when the app i
  • How instagram or direct not change shape in icon launcher to circle in android oreo
  • Android Studio not loading image when creating new image asset
  • Android toolbar overflow menu change text color and fill menu to the screen
  • problem with code to combine two images in android?
  • “class template has already been declared as a non-class template”
  • Tiny Images, No Rotation, but still get OutOfMemoryError: bitmap size exceeds VM budget
  • Android ripple for button not working
  • Android how to set a transparent image resource of a view? or Remove the image?
  • Change Design Support Navigation View Header Title Programmatically
  • write text on image and show it to a imageview
  • Unable to resolve Static method?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • extjs4 catching the scroll event on panel
  • there is no graph with tensorboard
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Word Open XML Mail Merge
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass