Question

I made a custom application that can setup background wallpapers for the android phone/tablet. Yet the only problem I face is that when I let the background be setup for my phone, it does not give me the resize option and expands the whole image.

Question: How can I activate this option, when you set the image you get automatic the resize option? Code here below:

ImageView display;
int toPhone; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.wallpaper);

    toPhone = R.drawable.droid;

    display = (ImageView) findViewById(R.id.IVdisplay); 
    ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
    ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
    ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
    ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
    ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById(R.id.IVimage10);
    Button setWall = (Button) findViewById(R.id.BsetWallpaper);

    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    setWall.setOnClickListener(this);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.IVimage1:
        display.setImageResource(R.drawable.droid);
        toPhone = R.drawable.droid;
        break;
    case R.id.IVimage2:
        display.setImageResource(R.drawable.droid1);
        toPhone = R.drawable.droid1;
        break;
    case R.id.IVimage3:
        display.setImageResource(R.drawable.droid2);
        toPhone = R.drawable.droid2;
        break;
    case R.id.IVimage4:
        display.setImageResource(R.drawable.droid3);
        toPhone = R.drawable.droid3;
        break;
    case R.id.IVimage5:
        display.setImageResource(R.drawable.droid4);
        toPhone = R.drawable.droid4;
        break;
    case R.id.IVimage6:
        display.setImageResource(R.drawable.droid5);
        toPhone = R.drawable.droid5;
        break;
    case R.id.IVimage7:
        display.setImageResource(R.drawable.droid6);
        toPhone = R.drawable.droid6;
        break;
    case R.id.IVimage8:
        display.setImageResource(R.drawable.droid7);
        toPhone = R.drawable.droid7;
        break;
    case R.id.IVimage9:
        display.setImageResource(R.drawable.droid8);
        toPhone = R.drawable.droid8;
        break;
    case R.id.IVimage10:
        display.setImageResource(R.drawable.droid9);
        toPhone = R.drawable.droid9;
        break;
    case R.id.BsetWallpaper:
        InputStream yep = getResources().openRawResource(toPhone);
        Bitmap background = BitmapFactory.decodeStream(yep);
        try{
            getApplicationContext().setWallpaper(background);
        }catch(IOException e){
            e.printStackTrace();
        }
        break;
    }

}

}

Was it helpful?

Solution 2

The answer is a combined answer with that of loop and some other codes, now it works perfectly. So creds to Loop for the code you gave me :)

case R.id.BsetWallpaper:

        DisplayMetrics metrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // get the height and width of screen 
        int height = metrics.heightPixels; 
        int width = metrics.widthPixels;

        try {
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
            int targetWidth = wallpaperManager.getDesiredMinimumWidth();
            int targetHeight = wallpaperManager.getDesiredMinimumHeight();
            Bitmap source = BitmapFactory.decodeResource(getResources(), toPhone);
            int sourceWidth = source.getWidth();
            int sourceHeight = source.getHeight();
            Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(target);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(source, sourceWidth < targetWidth ? (targetWidth-sourceWidth)/2 : 0, sourceHeight < targetHeight ? (targetHeight-sourceHeight)/2 : 0, null);
            wallpaperManager.setBitmap(target);

                  wallpaperManager.suggestDesiredDimensions(width, height);
                 Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
        }catch(IOException e){
            e.printStackTrace();
        }
        break;

OTHER TIPS

First of all you are using deprecated method since API 5 for setting wallpaper. Use http://developer.android.com/reference/android/app/WallpaperManager.html#getInstance(android.content.Context) instead.

Secondly, if your image is smaller than screen size, Android has to fill other pixels with something. So I would suggest to create another bitmap with single color and on top of that draw your bitmap. This way you can control how your images are displayed. See my example below:

try {
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    int targetWidth = wallpaperManager.getDesiredMinimumWidth();
    int targetHeight = wallpaperManager.getDesiredMinimumHeight();
    Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(target);
    canvas.drawColor(Color.GRAY);
    canvas.drawBitmap(source, sourceWidth < targetWidth ? (targetWidth-sourceWidth)/2 : 0, sourceHeight < targetHeight ? (targetHeight-sourceHeight)/2 : 0, null);
    wallpaperManager.setBitmap(target);
} catch (IOException e) {
    e.printStackTrace();
}

Please note that I am using Config RGB_565 which does note have alpha(that's what you want).

Additionally, in case your images has high resolution it would be wise to optimise their loading to memory. Just have a look at Developers page http://developer.android.com/training/displaying-bitmaps/load-bitmap.html use code from their sample. Basically, with that code you will load images scaled to maximum screen size and prevent your app from OutOfMemoryError.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top