0

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.

1 Answers1

0

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) {
        }
    }
    }
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • Thanks Nick, I used the code you propose in this position, where the image appears on screen, after this line: imageView.setImageResource(mImagesIds[position]); and gives me this error "Cannot resolve symbol newsPager" and this "Cannot resolve symbol useThisBitmap" What i'm doing wrong? A friend say me need to create another activity with fullscreen and put a button. Is it true?. thanks and sorry for my English – toni jimenez Feb 02 '15 at 09:05
  • Thanks Nick, I used the code as you put in your post. and I put the button on the layout. When I try it just me an error on the line `mImageId int = imagesDescriptions [newsPager.getCurrentItem ()];` says **"error: incompatible types required: int found: String"** thanks and sorry for my English – toni jimenez Feb 03 '15 at 12:49
  • no, I get the error I mention above. **"error: incompatible types required: int found: String"** in the line `mImageId int = imagesDescriptions [newsPager.getCurrentItem ()];` – toni jimenez Feb 04 '15 at 09:25
  • write this "int mImageId = mImagesIds[newsPager.getCurrentItem()];" – NickUnuchek Feb 04 '15 at 09:41
  • Hi Nick, thanks again. No fails in the editor, but when I compile stops with this message `java.lang.RuntimeException: Unable to start activity ComponentInfo {dmdln.gridview_005 / dmdln.gridview_005.SwipeImageActivity}: java.lang.NullPointerException` I'll edit my answer to explain it better – toni jimenez Feb 05 '15 at 09:28
  • Is it on all pages when you clicking on button BTN_SET ? or only on first or last? – NickUnuchek Feb 05 '15 at 09:42
  • the failure occurs when the grid goes to swipe at the change of activity. fails to display the screen swipe – toni jimenez Feb 05 '15 at 11:50
  • Hi Nick, i changed the button from the show_images layout to the top of the swipe_layout and **works perfect**. thank you very much for everything. I'm sorry, I'm new and not let me vote for your response. When you come to Barcelona you have a few free beers – toni jimenez Feb 05 '15 at 13:58
  • just set like to my answer and set the green checkbox near it. – NickUnuchek Feb 06 '15 at 09:30