I have a wallpaper app that contains a listview that has the images and you pick an image then set it as a wallpaper but I don't know how to tell which item was picked by the user then I have a broadcast receiver so the wallpaper won't change after powering of the device but I also don't know how to tell it which item was picked.
here is my MainActivty:
public class Set1_Activity extends Activity {
ImageView display;
ListView list;
Integer[] images = {
R.drawable.iv1_s, R.drawable.iv2_s, R.drawable.iv3_s, R.drawable.iv4_s,
.......
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
display = (ImageView) findViewById(R.id.IView);
list = (ListView) findViewById(R.id.setOneListView);
HeartlessAdapter adapter = new HeartlessAdapter(this, images);
list.setAdapter(adapter);
Button setWp = (Button) findViewById(R.id.setWp);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch(position){
case 0:
display.setImageResource(R.drawable.iv1);
break;
case 1:
display.setImageResource(R.drawable.iv2);
break;
case 2:
display.setImageResource(R.drawable.iv3);
break;
........
setWp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast WpSet = Toast.makeText(Set1_Activity.this,"Wallpaper Set", Toast.LENGTH_SHORT);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), //what should I write here );
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(Set1_Activity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
WpSet.show();
}
});
And Here is my broadcast Receiver:
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
@Override public void onReceive(Context context,Intent intent){
try{
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), //what should i type here);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(tempbitMap);
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
Why not using
preferences
, use this method inside yourMainActivity
Class to save the position:Create a class to save/retrieve preference values:
Saving Position:
Retrieving Position: