title: Set an image to wallpaper
body: <p>I am new to coding and never worked with it before! We are working with Android and Java eclipse, it's a school project. Now I am working with a band application were I am supposed to create the gallery. This I managed, but to create a function were the user can set an image to a wallpaper, I couldn't. I need some help with this! I believe I found good code, but I don't know how to implement it in my work. </p>
  
  <p>Does anyone know how I should connect these to each other? Really thankful for help,</p>
  
  <p>Sandra</p>
  
  <p><strong>My Gallery code:</strong> </p>
  
  <pre><code>package com.Lavin;
  
      import com.Lavin.R;
      import java.io.IOException;
      import android.app.Activity;
      import android.app.AlertDialog;
      import android.content.Context;
      import android.content.DialogInterface;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.view.ViewGroup;
      import android.view.Window;
      import android.view.animation.AnimationUtils;
      import android.widget.AdapterView;
      import android.widget.BaseAdapter;
      import android.widget.Gallery;
      import android.widget.Gallery.LayoutParams;
      import android.widget.ImageSwitcher;
      import android.widget.ImageView;
      import android.widget.ViewSwitcher;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.app.WallpaperManager;
      import android.graphics.Color;
      import android.graphics.PorterDuff;
      import android.graphics.drawable.Drawable;
  
      public class Lavin extends Activity implements
      AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
  
  
      setContentView(R.layout.main);
  
      mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
      mSwitcher.setFactory(this);
      mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
              android.R.anim.fade_in));
      mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
              android.R.anim.fade_out));
  
      Gallery g = (Gallery) findViewById(R.id.gallery);
      g.setAdapter(new ImageAdapter(this));
      g.setOnItemSelectedListener(this);
  }
  
  public void onItemSelected(AdapterView parent, View v, int position, long id) {
      mSwitcher.setImageResource(mImageIds[position]);
  
  }
  
  
  public void onNothingSelected(AdapterView parent) {
  }
  
  public View makeView() {
      ImageView i = new ImageView(this);
      i.setBackgroundColor(0xFF000000);
      i.setScaleType(ImageView.ScaleType.FIT_CENTER);
      i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
              LayoutParams.MATCH_PARENT));
      return i;
  }
  
  private ImageSwitcher mSwitcher;
  
  public class ImageAdapter extends BaseAdapter {
      public ImageAdapter(Context c) {
          mContext = c;
      }
  
      public int getCount() {
          return mThumbIds.length;
      }
  
      public Object getItem(int position) {
          return position;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView i = new ImageView(mContext);
  
          i.setImageResource(mThumbIds[position]);
          i.setAdjustViewBounds(true);
          i.setLayoutParams(new Gallery.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          i.setBackgroundResource(R.drawable.picture_frame);
          return i;
      }
  
      private Context mContext;
  
  }
  
  private Integer[] mThumbIds = {
          R.drawable.lavin_thumb_0, R.drawable.lavin_thumb_1,
          R.drawable.lavin_thumb_2, R.drawable.lavin_thumb_3,
          R.drawable.lavin_thumb_4, R.drawable.lavin_thumb_5,
          R.drawable.lavin_thumb_6, R.drawable.lavin_thumb_7,
          R.drawable.lavin_thumb_8, R.drawable.lavin_thumb_9,
          R.drawable.lavin_thumb_10, R.drawable.lavin_thumb_11,
          R.drawable.lavin_thumb_12};
  private Integer[] mImageIds = {
          R.drawable.lavin_0, R.drawable.lavin_1, R.drawable.lavin_2,
          R.drawable.lavin_3, R.drawable.lavin_4, R.drawable.lavin_5,
          R.drawable.lavin_6, R.drawable.lavin_7, R.drawable.lavin_8, 
          R.drawable.lavin_9, R.drawable.lavin_10,
          R.drawable.lavin_11, R.drawable.lavin_12};
  
  }
  </code></pre>
  
  <p><strong>My Wallpaper code:</strong></p>
  
  <pre><code>package com.Lavin;
  
  import android.app.Activity;
  import android.os.Bundle;
  
  import java.io.IOException;
  
  import android.app.Activity;
  import android.app.WallpaperManager;
  import android.graphics.Color;
  import android.graphics.PorterDuff;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  
  public class Wallpaper extends Activity {
       final static private int[] mColors =
       {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
               Color.YELLOW, Color.WHITE};
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.main);
          final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
          final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
          final ImageView imageView = (ImageView) findViewById(R.id.imageview);
          imageView.setDrawingCacheEnabled(true);
          imageView.setImageDrawable(wallpaperDrawable);
  
  
          Button randomize = (Button) findViewById(R.id.randomize);
          randomize.setOnClickListener(new OnClickListener() {
              public void onClick(View view) {
                  int mColor = (int) Math.floor(Math.random() * mColors.length);
                  wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
                  imageView.setImageDrawable(wallpaperDrawable);
                  imageView.invalidate();
              }
          });
  
          Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
          setWallpaper.setOnClickListener(new OnClickListener() {
              public void onClick(View view) {
                  try {
                      wallpaperManager.setBitmap(imageView.getDrawingCache());
                      finish();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  }
  </code></pre>