1

First of all I am new at android development. I am working on wallpaper app on that I want to add swipe able images. The working of wallpaper app is like ManiActivity in that all the categories are display and have on click listener whenever user click on any of the category wallpapers are shown in grid view from firebase database. After clicking on particular wallpaper it shows in fullimage activity in which wallpaper will shown on full screen with two button apply and download. In fullimage activity I want to add that swipe function. Means whenever user swipe left of right it will show next image respectively. Please guide me how I can do this. Help me to add swipe function. I have seen most of video tutorials of it but I did'nt understand. Please guide me it would be greatful more me.

MainActivity code:-

public class MainActivity extends AppCompatActivity {

private CardView animal;
private CardView birds;
private CardView marinelife;
private CardView bike;
private CardView car;
private CardView flowers;
private CardView cutebaby;
private CardView nature;
private CardView superhero;
private CardView themes;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    animal = findViewById(R.id.animal);
    birds = findViewById(R.id.birds);
    marinelife = findViewById(R.id.marinelife);
    bike = findViewById(R.id.bike);
    car = findViewById(R.id.car);
    flowers = findViewById(R.id.flowers);
    cutebaby = findViewById(R.id.cutebaby);
    nature = findViewById(R.id.nature);
    superhero = findViewById(R.id.superhero);
    themes = findViewById(R.id.themes);




    animal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent a = new Intent(getApplicationContext(), AnimalWallpaper.class);
            startActivity(a);

        }
    });

    birds.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent birds = new Intent(getApplicationContext(), BirdsWallpaper.class);
            startActivity(birds);
        }
    });

    marinelife.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent marine = new Intent(getApplicationContext(), MarinelifeWallpaper.class);
            startActivity(marine);
        }
    });


    bike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent b = new Intent(getApplicationContext(), BikeWallpaper.class);
            startActivity(b);
        }
    });

    car.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent c = new Intent(getApplicationContext(), CarWallpaper.class);
            startActivity(c);
        }
    });

    flowers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent g = new Intent(getApplicationContext(), FlowersWallpaper.class);
            startActivity(g);
        }
    });

    cutebaby.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent cute = new Intent(getApplicationContext(), CutebabyWallpaper.class);
            startActivity(cute);
        }
    });

    nature.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent n = new Intent(getApplicationContext(), NatureWallpaper.class);
            startActivity(n);
        }
    });

    superhero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent s = new Intent(getApplicationContext(), HereosWallpaper.class);
            startActivity(s);

        }
    });


    themes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent t = new Intent(getApplicationContext(), ThemesWallpaper.class);
            startActivity(t);

        }

    });

}

}

WallpaperAdapter Code:-

public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.WallpaperViewHolder> {

private ArrayList<String> list;
private Context context;

public WallpaperAdapter(ArrayList<String> list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@org.jetbrains.annotations.NotNull
@Override
public WallpaperViewHolder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.custom_image_layout, parent, false );

    return new WallpaperViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull WallpaperAdapter.WallpaperViewHolder holder, int position) {

    Glide.with(context).load(list.get(position)).into(holder.imageView);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(context, FullImageActivity.class);
            intent.putExtra("images", list.get(position));
            context.startActivity(intent);
        }
    });

}

@Override
public int getItemCount() {
    return list.size();
}

public class WallpaperViewHolder extends RecyclerView.ViewHolder {

    ImageView imageView;

    public WallpaperViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.item_image);
    }
}

}

FullImageActvity Code:-

public class FullImageActivity extends AppCompatActivity {

private ImageView fullimage;
private Button apply;
private Button download;
private String url;
Bitmap bitmap;
private String imageName = "wallpaper";

private static final int REQUEST_CODE = 100;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_image);





    fullimage = findViewById(R.id.fullImage);
    apply = findViewById(R.id.apply);
    download = findViewById(R.id.download);

    Picasso.get().load(url).into(fullimage);
    
    url = getIntent().getStringExtra("images");

    Glide.with(this).load(url).into(fullimage);

    apply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            setBackground();
            Toast.makeText(FullImageActivity.this, "Wallpaper Applied Sucessfully....", Toast.LENGTH_SHORT).show();
        }
    });


    download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                        PackageManager.PERMISSION_DENIED) {

                    String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    requestPermissions(permission,REQUEST_CODE);

                }

                else {
                    saveImage();
                }
            }


        }
    });
}

private void saveImage() {

    bitmap = ((BitmapDrawable)fullimage.getDrawable()).getBitmap();
    String time = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
    File path = Environment.getExternalStorageDirectory();
    File dir = new File(path+"/Free HD Wallpapers 2021");
    dir.mkdirs();
    String imagename = time+".PNG";
    File file = new File(dir,imagename);
    OutputStream out;

    try {

        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
        out.flush();
        out.close();
        Toast.makeText(FullImageActivity.this, "Image save in "+getString(R.string.app_name), Toast.LENGTH_SHORT).show();

    } catch (Exception e) {

        Toast.makeText(FullImageActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();

    }





}


private void setBackground() {

    Bitmap bitmap = ((BitmapDrawable)fullimage.getDrawable()).getBitmap();

    WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());

    try {
        manager.setBitmap(bitmap);
    } catch (IOException e) {
        Toast.makeText(this, "Error : "+e.getMessage(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }



}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

        case REQUEST_CODE: {

            if (grantResults.length > 0 && grantResults[0] ==

                    PackageManager.PERMISSION_GRANTED) {

            } else {
                Toast.makeText(this, "Permission Enable", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

}

Oneof the Category code:-

public class BikeWallpaper extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private DatabaseReference reference;
private ArrayList<String> list;
private WallpaperAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bike_wallpaper);


    reference = FirebaseDatabase.getInstance().getReference().child("bike");

    recyclerView = findViewById(R.id.recyclerViewBike);
    progressBar = findViewById(R.id.progressBarBike);

    getData();
}

private void getData() {
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

            progressBar.setVisibility(View.GONE);
            list = new ArrayList<>();

            for (DataSnapshot shot : snapshot.getChildren()) {

                String data = shot.getValue().toString();
                list.add(data);
            }

            recyclerView.setLayoutManager(new GridLayoutManager(BikeWallpaper.this, 2));
            adapter  = new WallpaperAdapter(list, BikeWallpaper.this);
            recyclerView.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);


        }

        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error) {

            progressBar.setVisibility(View.GONE);
            Toast.makeText(BikeWallpaper.this, "Error : "+error.getMessage() , Toast.LENGTH_SHORT).show();
        }
    });
}

}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If you encounter problems, it's best to create a [MCVE](https://stackoverflow.com/help/mcve) when posting a question. You posted almost **400 (four hundred)** lines of code for this issue. That's a lot for people to parse and try to debug online. Please edit your question and isolate the problem, in that way you increase your chances of being helped. – Alex Mamo Aug 06 '21 at 12:22
  • Besides that, if you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), I think that this article, [How to delete a record from Firestore on a RecylerView left/right swipe?](https://medium.com/firebase-tips-tricks/how-to-delete-a-record-from-firestore-on-a-recylerview-left-right-swipe-d65d993f0baf) or [How to delete multiple records from Firestore using RecyclerView multi-selection?](https://medium.com/firebase-tips-tricks/how-to-delete-multiple-records-from-firestore-using-recyclerview-multi-selection-96108e4c6166) might help. – Alex Mamo Aug 06 '21 at 12:23

1 Answers1

0

For the screen where you only see one image at a time and want to swipe through them, use ViewPager. See this example: https://github.com/chiuki/android-swipe-image-viewer/blob/master/src/com/sqisland/android/swipe_image_viewer/MainActivity.java

You would just want to use an ArrayList or something dynamic for holding all of your images instead of an array for your adapter. For the grid view screen, use a RecyclerView with GridLayoutManager.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Sorry, but I didn't understand can you tell me more about it how I can make images swipe able for particular child. My project contains 450+ wallpapers. And Thank You very much you replied. –  Aug 05 '21 at 13:45
  • There are some great answers here explaining how to use ViewPager to swipe through images: https://stackoverflow.com/questions/5101629/how-to-swipe-images – Nathan Meade Aug 05 '21 at 13:48
  • I have seen most of it but I didn't understand. Please help to get perfect solution I mean at what I have to make changes I mean in adapter or which activity. –  Aug 05 '21 at 14:56
  • Research RecyclerView and Adapters. Along with those two concepts it is important to incorporate GridLayoutManager for your RecyclerView and ViewPager for your ListView. Selecting an image in your RecyclerView could navigate you to a "WallpaperDetailsActivity" with an intent. Within that activity you could have your ListView displayed using a ViewPager adapter. If you understand how to implement these four things: 1. RecyclerView, 2. Adapters, 3. GridLayoutManager, 4. ViewPager <- you should be able to implement your solution. – Nathan Meade Aug 06 '21 at 02:35
  • 1
    Okk. Thanak You So much for your reply. –  Aug 06 '21 at 05:04
  • Yes, no problem. – Nathan Meade Aug 06 '21 at 05:32