2

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.

John
  • 3,769
  • 6
  • 30
  • 49
Santanu
  • 893
  • 3
  • 10
  • 24
  • Please provide the error that's thrown in `.setBackground`, and look at what `Drawable.createFromPath()` returns (is it indeed a drawable? Did `createFromPath` succeed?) – AlexWalterbos Apr 19 '15 at 21:15
  • There's no error thrown actually, only thing is that when i am trying to use Drawable.createFrompath(), its saying me minimum API level required is 16 and you are using lesser than that. however the color thing is working now. Only i am not being able to change the background. What i canged by this time was: 1. I added an id to the Layout tag of my xml and then used that id to change the color. 2. But i cannot do the sam ething for background(Drwawable) becuase it expects a drawable argument which i am not being able to provide and please not using your above method its NOT WORKING!! – Santanu Apr 19 '15 at 21:22
  • Use `getResources().getDrawable(R.drawable.your_drawable)` and put your drawable (the jpg) in the `res/drawable` folder. You cannot use `setBackground` in API levels under 16, you should use `setBackgroundDrawable` for that. [Source and example on API level-dependent execution](http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android) – AlexWalterbos Apr 19 '15 at 21:27

3 Answers3

4

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);
Musa Y.
  • 1,747
  • 18
  • 26
  • Thank you M_Y. It worked, but as i saw your solution i thought why i was not using this method yet. Instead of using these two lines of yours i just changed my setBackground(Drawable) to : activity.findViewById(android.R.id.content).setBackgroundResource(R.id.love1); – Santanu Apr 19 '15 at 21:32
  • Glad it worked! Yes, that's another way of doing it. – Musa Y. Apr 19 '15 at 21:35
0

Change setBackground(Drawable) to setBackgroundResource(R.drawable.filename)

Santanu
  • 893
  • 3
  • 10
  • 24
0

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

Safdar Pathan
  • 11
  • 1
  • 7