Code example for Gallery

Methods: getSelectedItemPosition

0
 
            View setButton = view.findViewById(R.id.set);
            setButton.setOnClickListener(new OnClickListener() {
                @Override 
                public void onClick(View v) {
                    selectWallpaper(gallery.getSelectedItemPosition());
                } 
            }); 
            return view;
        } 
        return null; 
    } 
 
    private void selectWallpaper(int position) {
        try { 
            WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
                    Context.WALLPAPER_SERVICE);
            wpm.setResource(mImages.get(position));
            Activity activity = getActivity();
            activity.setResult(Activity.RESULT_OK);
Contextual code suggestions in your IDE  Get Codota for Java