I am having an Application Containing Different Images using ImageView and ViewPager. I want to set the current image shown through ImageView as Wallpaper.
But it only makes the first image as wallpaper and remaining images when I tried to set as wallpaper it makes the mobile screen black (means it makes wallpaper black) it does not set other images from ImageView as Wallpaper.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.testimagesbaby.BabyBoys"
tools:showIn="@layout/activity_baby_boys"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.15"
>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</LinearLayout>
Java File:
public class BabyBoys extends AppCompatActivity {
ViewPager viewPager;
private AlertDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby_boys);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter viewPagerAdapter= new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
}
public static Bitmap viewToBitmap (View view, int width, int height){
Bitmap bitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas= new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_boys_tab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_setwall) {
setwall(); }
else if (id == R.id.action_save) {
}
return super.onOptionsItemSelected(item);
}
public void setwall()
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(viewToBitmap(viewPager, viewPager.getWidth(),viewPager.getHeight()));
Toast.makeText(this, "Sucessfully Wallpaper Set", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
}
}
ViewPagerAdapter File
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer [] images= {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6,R.drawable.b7,R.drawable.b8,R.drawable.b9,R.drawable.b10,R.drawable.b11};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position){
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);
ImageView imageView=(ImageView) view.findViewById(R.id.imageView2);
imageView.setImageResource(images[+position]);
ViewPager vp= (ViewPager) container;
vp.addView(view,0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){
ViewPager vp= (ViewPager) container;
View view= (View) object;
vp.removeView(view);
}
}
You need to set the current image to the wallpaper manager.
Do the following changes
wallpaperManager.setBitmap(viewToBitmap(viewPager,viewPager.getWidth(),viewPager.getHeight()));
use
myWallpaperManager.setResource(images[currentImagePos]);