9

I have been able to change colour of an activity background (see this post). Now a requirement is to do the same with background image. I mean I can click a button, select an option and change the current Activity background image to the new one.

Here is what I have done:

private SharedPreferences prefs;    
private static final String SELECTED_ITEM = "SelectedItem"; 
private Editor sharedPrefEditor;

btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    final CharSequence[] items={getString(R.string.default),getString(R.string.pix1), getString(R.string.pix2))};
    AlertDialog.Builder builder = new AlertDialog.Builder(
            ContentView.this);

    builder.setTitle((getResources().getString(R.string.color_switch)));
    builder.setPositiveButton((R.string.ok), new DialogInterface.OnClickListener() { 

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {                
            wvContent = (WebView) findViewById(R.id.wvContent);             
            int bg_color=0;

            if(getString(R.string.default).equals(items[which]))
            {                   
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.default);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.default; 
            }
            else if(getString(R.string.pix1).equals(items[which]))
            {
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix1);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.pix1;
                }
            else if(getString(R.string.pix2).equals(items[which]))
            {
                wvContent.setBackgroundColor(0);
                BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix2);
                bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
                wvContent.setBackgroundDrawable(bg);                    
                bg_color=R.drawable.pix2;                   
                }               
            saveSelectedItem(bg_color);
        }
    });
    builder.show();

Changes are saved and loaded using the following code:

//OnCreate
wvContent = (WebView) findViewById(R.id.wvContent); 
wvContent.setBackgroundColor(getSelectedItem());
...
private int getSelectedItem() {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    return prefs.getInt(SELECTED_ITEM, -1);
}

private void saveSelectedItem(int which) {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    sharedPrefEditor = prefs.edit();
    sharedPrefEditor.putInt(SELECTED_ITEM, which);
    sharedPrefEditor.commit();
}

The Activity background image does change when it is selected from the Dialog list, BUT the change is not saved and loaded next time when the activity is relaunched.

I have no idea now how to solve this problem. Can you please help? Many thanks.

6
  • kindly explain to me,u want the image change from the user side?when ever user want he will change the image??am i correct? Mar 24, 2014 at 7:28
  • Yes. This is kind of reading material, and the user may want to change the background to meet their eyesight need. Mar 24, 2014 at 8:24
  • ok then u need to put that image into sdcard..and when ever user need to change the image,he can only change or replace the image within his sdcard..so i mean the image will be change dunamically from the client side..can u get my point and sorry for my english.. Mar 24, 2014 at 8:44
  • That may be the next step. At this point, I only focus on how to save and retain the background change so that when the app (or activity) starts next time, the background image is still there. Mar 24, 2014 at 8:50
  • This is the advice please lets try and let me know if it's worked or not...in your saveSelectedItem(int which) method please remove the following lines and implement within onCreate method plz try.... if (prefs == null) { prefs = PreferenceManager .getDefaultSharedPreferences(this); } Mar 24, 2014 at 8:58

3 Answers 3

12

When you are setting background after selecting from Dialog then you are getting the resource id R.drawable.pix2 and retrieving the BitmapDrawable as follows...

wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.pix2);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);                    
bg_color=R.drawable.pix2;

But in onCreate() method you are just passing the resource id as below...

wvContent.setBackgroundColor(getSelectedItem());

where, getSelectedItem() returns an int value which is a resource id.

Now, set background drawable as follows in onCreate() method...

wvContent.setBackgroundColor(0);
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(getSelectedItem());
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
wvContent.setBackgroundDrawable(bg);

You can update background from SDCard as follows...

    String pathName = Environment.getExternalStorageDirectory().getPath() + "/folder/" + "image.jpg";
    Resources res = getResources(pathName);
    Bitmap bitmap = BitmapFactory.decodeFile(pathName);
    BitmapDrawable backgroundDrawable = new BitmapDrawable(res, bitmap);
    wvContent.setBackgroundDrawable(backgroundDrawable);
9
  • 1
    @HS that's what i m talk in about. +1 up for your Awesome answer
    – M D
    Mar 24, 2014 at 7:31
  • This appears the most logical. I have tried your instruction but there's no luck. The activity still displays its default background after it relaunches. Mar 24, 2014 at 8:45
  • 1
    to choose image from sdcard try the following code... String pathName = "/sdcard/nogastorebgimage.png"; Resources res = getResources(); Bitmap bitmap = BitmapFactory.decodeFile(pathName); BitmapDrawable bd = new BitmapDrawable(res, bitmap); RelativeLayout rl_main = (RelativeLayout) findViewById(R.id.rl_main); rl_main.setBackgroundDrawable(bd); Mar 24, 2014 at 9:27
  • 1
    @HamidShatu +1 from me for ur nice answer :) Mar 24, 2014 at 9:52
  • 1
    @NiamhDoyle ...I think, this link will help you more: viralpatel.net/blogs/pick-image-from-galary-android-app Mar 24, 2014 at 10:46
4

add this to your activity.xml:

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/iso"
        android:id="@+id/background"
        android:scaleType="fitXY"
        />

add this to activity.java:

ImageView layout = (ImageView) findViewById(R.id.background);
            layout.setBackgroundResource(R.drawable.iso);
2
  • And how this is used to change the background if user wants? Mar 24, 2014 at 7:24
  • when set ImageView as is it will become background of activity Mar 24, 2014 at 7:27
0
webView.setBackgroundColor(0);
WebView.setBackgroundResource(R.drawable.yourImage);

use this above code, may this help you....

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.