Gallery Application Java Code
Java Code Gallay Application
package com.example.gallery;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Integer imgarr[] = {
R.drawable.img1,R.drawable.img2,R.drawable.img3,
R.drawable.img4,R.drawable.img5,R.drawable.img6,R.drawable.img7
};
int currimg = 4;
public void previous(View view){
if(currimg == 0){
currimg = 4;
}
currimg--;
ImageView iv = (ImageView)findViewById(R.id.imagedsply);
iv.setImageResource(imgarr[currimg]);
}
public void next(View view){
if(currimg == 6){
currimg = 4;
}
currimg++;
ImageView iv = (ImageView)findViewById(R.id.imagedsply);
iv.setImageResource(imgarr[currimg]);
}
//Setting Wallpaper
private void setWallpaper(){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgarr[currimg]);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try{
manager.setBitmap(bitmap);
Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.wallbtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setWallpaper();
}
});
}
}
Comments
Post a Comment