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
试试this.findViewById(android.R.id.content).setBackgroundDrawable(d);
First give id to your layout
second findviewbyid that layout
third layout.setBackgroundResource(getResources().getDrawable(R.drawable.ready));
problem solve !!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.