I have done implementing "set wallpaper as home screen".but im not getting how can I set wallpaper as lock screen and both(home and lock).in following code "if" part is for setting as homescreen..rest is for "lock" and "both(home and lock)"..what will the logic for it to set wallpaper as lock and both(home and lock).?
code:
public class ViewImage extends AppCompatActivity {
Button button1, button2;
public List<RetroPhoto> product_lists=new ArrayList<>();
private RecyclerView recyclerView;
WallpaperManager myWallpaperManager;
public static FavoriteDatabase favoriteDatabase;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewimage_layout);
ImageView mPlace = findViewById(R.id.imagewall);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
ImageButton back = (ImageButton) findViewById(R.id.backToMain);
ImageButton downloadimage = findViewById(R.id.downloadimg);
ImageView favimg = findViewById(R.id.favimg);
recyclerView = findViewById(R.id.rec);
favoriteDatabase = Room.databaseBuilder(getApplicationContext(), FavoriteDatabase.class, "myfavdb").allowMainThreadQueries().build();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
assert navigationView != null;
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
final Bundle bundle = this.getIntent().getExtras();
final String imgUrl = bundle.getString("Title");
Glide.with(this).load(imgUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mPlace);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ViewImage.this);
// Set the alert dialog title
builder.setTitle("Set Wallpaper");
// Initialize a new string array of flowers
final String[] flowers = new String[]{
"Home Screen",
"Lock screen",
"Both"
};
// Set a list for alert dialog
builder.setItems(
flowers, // Items array
new DialogInterface.OnClickListener() // Item click listener
{
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Get the alert dialog selected item's text
String selectedItem = Arrays.asList(flowers).get(i);
if(selectedItem.equals(flowers[0])) {
Glide.with(ViewImage.this)
.load(imgUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
try {
WallpaperManager.getInstance(getApplicationContext()).setBitmap(resource);
Toast.makeText(ViewImage.this, "successfully set as home wallpaper", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
else if(selectedItem.equals(flowers[1]))
{
Uri bitmap = MediaStore.Images.Media.getContentUri(imgUrl);
final Uri finalBitmap = bitmap;
try {
WallpaperManager.getInstance(getApplicationContext()).setBitmap(finalBitmap,null,false,WallpaperManager.FLAG_LOCK);
// myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
Toast.makeText(ViewImage.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
//Toast.makeText(ViewImage.this, "successfully set as Lock wallpaper", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(ViewImage.this, "successfully set as both", Toast.LENGTH_LONG).show();
}
}
});
// Create the alert dialog
AlertDialog dialog = builder.create();
// Get the alert dialog ListView instance
ListView listView = dialog.getListView();
// Set the divider color of alert dialog list view
listView.setDivider(new ColorDrawable(Color.RED));
// Set the divider height of alert dialog list view
listView.setDividerHeight(0);
// Finally, display the alert dialog
dialog.show();
}
});
}
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
Check my code if its working for you :)
WallpaperManager myWallpaperManager;
private static final int CHOOSE_IMAGE = 22;
private Button btnSetWallpaper;
String[] options = new String[]{
"Home Screen",
"Lock Screen",
"Both"
};
In OnCreate()
btnSetWallpaper = findViewById(R.id.btnSetWallpaper);
btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "choose image"), CHOOSE_IMAGE);
}
});
In OnActivityResult()
if (requestCode == CHOOSE_IMAGE && data != null) {
mCropImageUri = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCropImageUri);
Log.e(TAG, "onActivityResult: BIT " + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose from below");
final Bitmap finalBitmap = bitmap;
builder.setItems(options, new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String selectedItem = Arrays.asList(options).get(i);
if (selectedItem.equals(options[0])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
} else if (selectedItem.equals(options[1])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
} else if (selectedItem.equals(options[2])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
}
}
});
dialog = builder.create();
dialog.show();
}
You wanna just set Flags for various Wallpaper Options. Flags like...
WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM : for both Screen
WallpaperManager.FLAG_SYSTEM : for Home Screen
WallpaperManager.FLAG_LOCK :for Lock Screen
Thanks :)