title: Set Imageview as wallpaper with a Button
body: <p>I have a problem with my code, I want to set an image from an <code>ImageView</code> as the wallpaper, I have a gallery and when I press an image from there the <code>ImageView</code> changes, so I don't know where to put the code for <code>onClick</code> to my button.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public class MainActivity extends Activity{
  
      final Integer[] mThumbIds = {
              R.drawable.a_compressed, R.drawable.b_compressed,
              R.drawable.c_compressed, R.drawable.d_compressed,
             };
      final Integer[] mFullSizeIds = {
              R.drawable.a, R.drawable.b,
              R.drawable.c, R.drawable.d,
             };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Gallery galeria = (Gallery) findViewById(R.id.gallery);
          galeria.setAdapter(new ImageAdapter(this));
          galeria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, final int position, long id) {
                  ImageView laimagen = (ImageView) findViewById(wallpaper);
                  laimagen.setImageResource(mFullSizeIds[position]);
              }
          });
      }
  
  
      public class ImageAdapter extends BaseAdapter {
          private Context mContext;
  
          public ImageAdapter(MainActivity inicio) {
              mContext = inicio;
          }
  
          public int getCount() {
              return mThumbIds.length;
          }
  
          public Object getItem(int position) {
               return null;
          }
  
          public long getItemId(int position) {
               return mFullSizeIds[position];
          }
  
          // create a new ImageView for each item referenced by the Adapter
          public View getView(int position, View convertView, ViewGroup parent) {
  
              ImageView  imageView = new ImageView(mContext);
              imageView.setImageResource(mThumbIds[position]);
              imageView.setLayoutParams(new Gallery.LayoutParams(180, 170));
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              return imageView;
  
          }
      }
  }
  </code></pre>
  
  <p>I tried this but it did not work</p>
  
  <pre><code>private void onClick() {
      Bitmap bitmap;
      bitmap = BitmapFactory.decodeResource(getResources(),mFullSizeIds[position]);
  
      try {
          getApplicationContext().setWallpaper(bitmap);
          Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_LONG).show();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This is my layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  android:background="#222222"
  android:orientation="vertical"&gt;
  
  
  &lt;ImageView android:id="@id/wallpaper"
      android:layout_width="fill_parent"
      android:layout_height="0dp"
      android:scaleType="centerInside"
      android:layout_weight="1.0"
      android:src="@drawable/a" /&gt;
  
  &lt;Gallery android:id="@id/gallery"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" /&gt;
  
  &lt;Button android:layout_gravity="center_horizontal"
      android:id="@id/set"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/SetWallpaper"
      android:background="@color/Esmeralda"
      android:textColor="@color/Clouds"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>