0

I have a requirement of changing the background of the current activity after some USER ACTIOn

Following id the Code i have written

Drawable d = new BitmapDrawable(getResources(),readBitmap(Uri.fromFile(new File(getFilesDir(), imageName))));
this.findViewById(android.R.id.content).setBackground(d);
this.findViewById(android.R.id.content).refreshDrawableState();
public Bitmap readBitmap(Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1; // reduce quality
    ParcelFileDescriptor pFileDescriptor = null;
    try {
        //fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
        pFileDescriptor = this.getContentResolver().openFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(pFileDescriptor.getFileDescriptor(), null, options);
            pFileDescriptor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bm;
}

Here is the content.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_img_body"
    tools:context=".MainActivity" >

Also if i remove the backgroung set in the XML it works just fine!! am confused

MOdified XML (Working)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity" >

Can anyone Please suggest.

Thanks is advance

2 Answers 2

1

try this.findViewById(android.R.id.content).setBackgroundDrawable(d);

Sign up to request clarification or add additional context in comments.

5 Comments

tried..Not worked.. BTW have edited the POST to add some info
Edited!..:-)..Please let me know if you need any other info
try using id in relative layout and change background of it using relativeLayout id.
i cant do that!.. bcoz i have written this code in a parent class(BASEACTIVITY)..Which is extended by all my activities...So the id keeps changing
method setBackgroundDrawable() is deprecated
0

First give id to your layout

second findviewbyid that layout

third layout.setBackgroundResource(getResources().getDrawable(R.drawable.ready));

problem solve !!!

1 Comment

i cant do that!.. bcoz i have written this code in a parent class(BASEACTIVITY)..Which is extended by all my activities...So the id keeps changing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.