0

The code contains a gallery with one button (which has to be added into your manifest file), I just need a suggestion on the button action; what to put inside "imageIDs* or bitmap to reference to the image which previously is clicked and shown.

Here is the complete code:

package net.keivan.gallery;

import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity { 
//---the images to display---
Integer[] imageIDs = {
        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.pic6,
        R.drawable.pic8,
        R.drawable.pic9,
        R.drawable.pic10,


};








/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {

            //---Displays the name of images as just as i click on them---

            Toast.makeText(getBaseContext(),
                    "pic" + (position + 1) + " selected",
                    Toast.LENGTH_SHORT).show();

            //---display the images selected---
            ImageView imageView = (ImageView) findViewById(R.id.image1);
            imageView.setImageResource(imageIDs[position]);


        }
    });
}


//---Set Wallpaper button---

public void onClick(View v) {




    try {


/*at this part can you suggest what to put inside "imageIDs* or *bitmap* to reference 
          to the image which previously is clicked and shown*/

/* first.the activity is created second.the image which i clicked is shown third.whin
           the user click on the button the image which previously is clicked on is set as background.*/




                    WallpaperManager.getInstance(this).setResource(imageIDs[position]);

                    //WallpaperManager.getInstance(this).setBitmap(mBitmap);



    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();
    }

}


///////////////////////////////////////////////

public class ImageAdapter extends BaseAdapter
{
    Context context;
    int itemBackground;

    public ImageAdapter(Context c)
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);            

        itemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);                

        a.recycle();
    }


    { getApplicationContext().getWallpaper(); }
    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }


    //---returns the item---
    public Object getItem(int position) {
        return position;
    }

     //---returns the ID of an item---
    public long getItemId(int position) {
        return position;
    }      

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setImageResource(imageIDs[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));                  
        } else {
            imageView = (ImageView) convertView;
        }            
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}

}
parreirat
  • 715
  • 3
  • 9
  • 22
user1952003
  • 5
  • 1
  • 6

1 Answers1

1
try {
    // Set background from a resource
    WallpaperManager.getInstance(this).setResource(imageIDs[position]);
    // or set background from a bitmap
    //WallpaperManager.getInstance(this).setBitmap(mBitmap);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

In your manifest file:

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • There is also another issue about the picture;as you mentioned there are two type to set backgruond 1.byimageIDs 2. by mBitmaps.so i need your suggestion if you would please, on how can i put **the id** or **the name of the image** which i previously selected in the first part. Thanx in advance – user1952003 Jan 06 '13 at 02:13
  • With the code you shared I cannot say more than : WallpaperManager.getInstance(this).setResource(imageIDs[position]); – Waza_Be Jan 06 '13 at 06:59
  • oh Well i just post the complete source. any suggestion on the button action;what to put inside "imageIDs* or bitmap to reference to the image which previously is clicked and shown. – user1952003 Jan 06 '13 at 15:31