Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically change the background image of an Android Activity

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.

like image 966
Niamh Doyle Avatar asked Mar 24 '14 07:03

Niamh Doyle


2 Answers

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);
like image 105
Hamid Shatu Avatar answered Oct 16 '22 07:10

Hamid Shatu


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);
like image 41
Mohammad Rababah Avatar answered Oct 16 '22 06:10

Mohammad Rababah