I have a problem with my code, I want to set an image from an ImageView
as the wallpaper, I have a gallery and when I press an image from there the ImageView
changes, so I don't know where to put the code for onClick
to my button.
This is my 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<?> 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;
}
}
}
I tried this but it did not work
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();
}
}
This is my layout:
<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">
<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" />
<Gallery android:id="@id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<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" />
</LinearLayout>
First of all you should take an int variable which specifies basically the index of drawableIds present in your mFullSizeIds[] and intialise it to -1 intitially like this
private int index=-1;
and in your galeria.setOnItemClickListener(), assign the position to your index variable like this.
galeria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
index=position;
laimagen.setImageResource(mFullSizeIds[position]);
}
});
and then in your Button click, do something like this
private void onClick() {
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity .this);
Drawable drawable = getResources().getDrawable(mFullSizeIds[index]);// here index is position of selected drawable in your mFullSizeIds[] that you have set in galeria.setOnItemClickListener()
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
your onCreate() will look like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView laimagen = (ImageView) findViewById(R.id.wallpaper);
Gallery galeria = (Gallery) findViewById(R.id.gallery);
galeria.setAdapter(new ImageAdapter(this));
galeria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
index=position;
laimagen.setImageResource(mFullSizeIds[position]);
}
});
Button setButton=(Button)findViewById(R.id.set);
setButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onClick();
}
});
}