Search code examples
androidimagebackgroundonclickwallpaper

set background image code


  ImageView im1_b;
    im1_b = (ImageView)findViewById(R.id.b_01_b);
    im1_b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(Next_2Activity.this);
            builder.setTitle("Project Gmelon");
            builder.setMessage("Are you sure set Wallpaper?");
            builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
               Toast.makeText(getApplicationContext(),
                       "You Select NO", Toast.LENGTH_LONG) .show();
                }
            });
            builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

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

                    WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());
                   try {
                    myWallpaperManager.setResource(R.drawable.b_01_i);
                   } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                   }
                    Toast.makeText(getApplicationContext(),
                           "Setting Complete, Thanks.", Toast.LENGTH_LONG) .show();
                }

            });

            builder.show();
        }
    });

I wrote code like this to set wallpaper.. but

my image is setting like this. (http://wemakeucc.com/1.jpg)

and I want like this.. (http://wemakeucc.com/2.JPG)

how can I do..?

EDIT1>

ImageView im1_b;
im1_b = (ImageView)findViewById(R.id.b_01_b);
im1_b.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
im1_b.setOnClickListener(new OnClickListener() {

Solution

  • If you want to do it in code you can do this:

    YOUR_IMAGE_VIEW.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    

    You can optionally experiment with other scale types instead of CENTER_INSIDE if you like!

    If you want to do it in xml, set the scaleType property with you desired scale type:

    android:scaleType="centerInside"
    

    Check this link for scale types: Scale Types