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.

Community
  • 1
  • 1
Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42
  • 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? – Farhan Shah Mar 24 '14 at 07:28
  • Yes. This is kind of reading material, and the user may want to change the background to meet their eyesight need. – Niamh Doyle Mar 24 '14 at 08: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.. – Farhan Shah Mar 24 '14 at 08: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. – Niamh Doyle Mar 24 '14 at 08: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); } – Farhan Shah Mar 24 '14 at 08:58
  • Please see @Hamid Shatu below. It's a possible solution. – Niamh Doyle Mar 24 '14 at 09:12

3 Answers3

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);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • 1
    @HS that's what i m talk in about. +1 up for your _Awesome_ answer – M D Mar 24 '14 at 07: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. – Niamh Doyle Mar 24 '14 at 08:45
  • Then you can try first setting `wvContent.setBackgroundColor(0);` then continue above process. – Hamid Shatu Mar 24 '14 at 08:50
  • Many thanks to you, @Hamid Shatu. That does the trick: I works now as I expect. I will accept yours as the answer, but if you don't mind, could you show me how to choose image as background from `sdcard` instead of `drawable`? – Niamh Doyle Mar 24 '14 at 09:10
  • 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); – Farhan Shah Mar 24 '14 at 09:27
  • 1
    @HamidShatu +1 from me for ur nice answer :) – Farhan Shah Mar 24 '14 at 09:52
  • Many thanks. The update really helps, but I mean to choose a particular image from sdcard of the user's preference, not a pre-defined. – Niamh Doyle Mar 24 '14 at 09:54
  • To choose image dynamically from SDCard, this link may help you: http://stackoverflow.com/questions/7856959/android-file-chooser – Hamid Shatu Mar 24 '14 at 10:02
  • 1
    @NiamhDoyle ...I think, this link will help you more: http://viralpatel.net/blogs/pick-image-from-galary-android-app/ – Hamid Shatu Mar 24 '14 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);
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
0
webView.setBackgroundColor(0);
WebView.setBackgroundResource(R.drawable.yourImage);

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

prakash
  • 1,413
  • 1
  • 21
  • 33