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();
}
});
}
}