Search code examples
androidgalleryswipewallpaper

on android. How to set wallpaper from swipe layout?


I have a swipe activity that also shows a small text. I want to use the image seen on the screen as wallpaper.

I learned a lot reading on this website. you helps a lot to everyone. This is my first visit, I could not find the answer by searching.

thanks and sorry for my poor English. This is the code

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;

public class SwipeImageActivity extends Activity {

public static Integer[] mImagesIds = {
        R.drawable.st1, R.drawable.st2,
        R.drawable.st3, R.drawable.st4,
        R.drawable.st5, R.drawable.st6,
        R.drawable.st7, R.drawable.st8,
        R.drawable.st9, R.drawable.st10,
        R.drawable.st11, R.drawable.st12,
        R.drawable.st13, R.drawable.st14,
        R.drawable.st15, R.drawable.st16,
        R.drawable.st17, R.drawable.st18,
        R.drawable.st10, R.drawable.st20,
        R.drawable.st21, R.drawable.st22,
        R.drawable.st23, R.drawable.st24,
};

private boolean hideSwipeText;
private String[] imagesDescriptions;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.swipe_images_layout);
    String i = getIntent().getStringExtra("position");
    int index = Integer.parseInt(i);

    imagesDescriptions = getResources().getStringArray(R.array.images_descriptions);

    SwipeImagePagerAdapter swipeNewsAdapter = new SwipeImagePagerAdapter();
    ViewPager newsPager = (ViewPager) findViewById(R.id.swipe_pager);
    newsPager.setAdapter(swipeNewsAdapter);
    newsPager.setCurrentItem(index);

    newsPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {
            ShowGalleryActivity.mSelected = i;
        }

        @Override
        public void onPageSelected(int i) {

        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
}

private class SwipeImagePagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return ShowGalleryActivity.mImagesIds.length;
    }

  @Override
    public Object instantiateItem(ViewGroup collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.show_images, null);
        LinearLayout swipeDescription = (LinearLayout) view.findViewById(R.id.swipe_description);

        if (hideSwipeText) {
            swipeDescription.setVisibility(View.GONE);
        }

        hideSwipeText = true;

        ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);

        imageView.setImageResource(mImagesIds[position]);

        TextView imageDescription = (TextView) view.findViewById(R.id.image_description);

        imageDescription.setText(imagesDescriptions[position].toString());

        collection.addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }


           @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }


    @Override
    public void finishUpdate(ViewGroup arg0) {
    }


    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(ViewGroup arg0) {
    }
}
}

when I write the last line of your response, I get the following error

Unhandled exception: java.io.IO.Exception wallpaperManager.setBitmap (bitmap);

I fix this way

    btn_set.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int mImageId = mImagesIds[newsPager.getCurrentItem()];

                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mImageId);
                    WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                    Drawable wallpaperDrawable = wallpaperManager.getDrawable();
                    try {
                        wallpaperManager.setBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

thats work if the button is in the swipe layout thanks Nick.


Solution

  • Do it:

    int mImageId = imagesDescriptions[newsPager.getCurrentItem()]
    
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mImageId );
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    Drawable wallpaperDrawable = wallpaperManager.getDrawable();
    wallpaperManager.setBitmap(useThisBitmap);
    

    Update:

    Make your "ViewPager newsPager" as a field of Activity and useThisBitmap - it's bitmap

     public class SwipeImageActivity extends Activity {
        ViewPager newsPager//this line added
        public static Integer[] mImagesIds = {
                R.drawable.st1, R.drawable.st2,
                R.drawable.st3, R.drawable.st4,
                R.drawable.st5, R.drawable.st6,
                R.drawable.st7, R.drawable.st8,
                R.drawable.st9, R.drawable.st10,
                R.drawable.st11, R.drawable.st12,
                R.drawable.st13, R.drawable.st14,
                R.drawable.st15, R.drawable.st16,
                R.drawable.st17, R.drawable.st18,
                R.drawable.st10, R.drawable.st20,
                R.drawable.st21, R.drawable.st22,
                R.drawable.st23, R.drawable.st24,
        };
    
        private boolean hideSwipeText;
        private String[] imagesDescriptions;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.swipe_images_layout);
            String i = getIntent().getStringExtra("position");
            int index = Integer.parseInt(i);
    
            imagesDescriptions = getResources().getStringArray(R.array.images_descriptions);
    
            SwipeImagePagerAdapter swipeNewsAdapter = new SwipeImagePagerAdapter();
            newsPager = (ViewPager) findViewById(R.id.swipe_pager);//this line changed
            newsPager.setAdapter(swipeNewsAdapter);
            newsPager.setCurrentItem(index);
            Button btn_set=(Button)findViewById(R.id.btn_set);//this line added and add it to layout
    //---added listner
             btn_set.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int mImageId = imagesDescriptions[newsPager.getCurrentItem()];
    
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mImageId );
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    Drawable wallpaperDrawable = wallpaperManager.getDrawable();
    wallpaperManager.setBitmap(bitmap);
                        }
                    });
    //---end of added listner
            newsPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i2) {
                    ShowGalleryActivity.mSelected = i;
                }
    
                @Override
                public void onPageSelected(int i) {
    
                }
    
                @Override
                public void onPageScrollStateChanged(int i) {
    
                }
            });
        }
    
        private class SwipeImagePagerAdapter extends PagerAdapter {
    
            @Override
            public int getCount() {
                return ShowGalleryActivity.mImagesIds.length;
            }
    
          @Override
            public Object instantiateItem(ViewGroup collection, int position) {
    
                LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                View view = inflater.inflate(R.layout.show_images, null);
                LinearLayout swipeDescription = (LinearLayout) view.findViewById(R.id.swipe_description);
    
                if (hideSwipeText) {
                    swipeDescription.setVisibility(View.GONE);
                }
    
                hideSwipeText = true;
    
                ImageView imageView = (ImageView) view.findViewById(R.id.gallery_image);
    
                imageView.setImageResource(mImagesIds[position]);
    
                TextView imageDescription = (TextView) view.findViewById(R.id.image_description);
    
                imageDescription.setText(imagesDescriptions[position].toString());
    
                collection.addView(view, 0);
    
                return view;
            }
    
            @Override
            public void destroyItem(ViewGroup collection, int position, Object view) {
                collection.removeView((View) view);
            }
    
    
                   @Override
            public boolean isViewFromObject(View view, Object object) {
                return (view == object);
            }
    
    
            @Override
            public void finishUpdate(ViewGroup arg0) {
            }
    
    
            @Override
            public void restoreState(Parcelable arg0, ClassLoader arg1) {
            }
    
            @Override
            public Parcelable saveState() {
                return null;
            }
    
            @Override
            public void startUpdate(ViewGroup arg0) {
            }
        }
        }