1

I'm using the picasso library for my app and it works very well so far. I have a image view and which dislpays image which are downloaded from web in a card layout. I want that when user clicks on image, it sets that image as wallpaper. (I'm a noob in this plz help me out, this is the last thing I'm struck in).

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_REQUEST_CODE = 1000 ;


    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    List<Item> items;
    CustomAdapter adapter;


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull     String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case PERMISSION_REQUEST_CODE:
        {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this,"Permission Denied", Toast.LENGTH_SHORT).show();
            }
            break;

        }
    }




    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String str="Click on Wallpaper to set  Wallpaper";
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
        mDrawerLayout=(DrawerLayout) findViewById(R.id.dl);
        mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView navigationView = (NavigationView) findViewById(R.id.Navigation_v);
        setupDrawerContent(navigationView);
        ActionBar actionBar = getSupportActionBar();

        recyclerView =(RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        initItem();

        //start service and play music
        startService(new Intent(MainActivity.this, SoundService.class));
    }
    private void  initItem() {

        items = new ArrayList<>();

        items.add(new Item(0,"https://images8.alphacoders.com/532/thumb-1920-532407.jpg"));
        items.add(new Item(1, "https://images5.alphacoders.com/394/thumb-1920-394511.jpg"));
        items.add(new Item(1,"https://images5.alphacoders.com/408/thumb-1920-408539.jpg"));

        adapter = new CustomAdapter(this,items);
        recyclerView.setAdapter(adapter);    
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onDestroy() {
        //stop service and stop music
        stopService(new Intent(MainActivity.this, SoundService.class));
        super.onDestroy();
    }
    public void selectItemDrawer(MenuItem menuItem){
        Fragment myFragment = null;
        Class fragmentClass;
        switch (menuItem.getItemId()) {

            case  R.id.walkthrough:
                fragmentClass = Walkthrough.class;
                break;
            case R.id.Fav:
                fragmentClass = Favourites.class;
                break;
            case R.id.info:
                fragmentClass = About.class;
                break;
            default:
                fragmentClass = Walkthrough.class;


        }
        try {
            myFragment = (Fragment) fragmentClass.newInstance();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
        setTitle(menuItem.getTitle());
        mDrawerLayout.closeDrawers();
    }
    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectItemDrawer(item);
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, MainActivity.class));
    }
}

Custom view holder

 class CustomViewHolder extends RecyclerView.ViewHolder{

RibbonLayout ribbonLayout;
ImageView imageView;

public CustomViewHolder(View itemView) {
    super(itemView);

    ribbonLayout = (RibbonLayout)itemView.findViewById(R.id.ribbonLayout);
    imageView = (ImageView)itemView.findViewById(R.id.imageView);

}
 }

CustomAdapter

 public class CustomAdapter extends  RecyclerView.Adapter<CustomViewHolder>{

Context context;
List<Item> itemList;

public CustomAdapter(Context context, List<Item> itemList) {
    this.context = context;
    this.itemList = itemList;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
    return new CustomViewHolder(itemView);

}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Item item = itemList.get(position);
    if (item.type == 0) { //new
        holder.ribbonLayout.setShowBottom(true);
        holder.ribbonLayout.setShowBottom(true);

        holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("white"));
        holder.ribbonLayout.setHeaderTextColor(Color.parseColor("white"));


        Picasso.with(context).load(item.imageURL)
                .into(holder.imageView);
    } else
        if (item.type == 1) { //hot
        holder.ribbonLayout.setShowBottom(false);
        holder.ribbonLayout.setShowBottom(false);
            holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("white"));
            holder.ribbonLayout.setHeaderTextColor(Color.parseColor("white"));




        Picasso.with(context).load(item.imageURL)
                .into(holder.imageView);
    }
    else
        if (item.type == 2) { //hot
            holder.ribbonLayout.setShowBottom(false);
            holder.ribbonLayout.setShowBottom(false);


            Picasso.with(context).load(item.imageURL).into(holder.imageView);

        }

}




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

}

Anish Jain
  • 231
  • 3
  • 12

2 Answers2

0

You can set the wallpaper with the result from picasso.

Bitmap result=Picasso.with(context)
      .load(imageURL)
      .get();

WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
try {
    wallpaperManager.setBitmap(result);
} catch (IOException error) {
    error.printStackTrace();
}
Kelly C.
  • 15
  • 6
  • I want it to download from the image view. I have many links set up and they all get loaded in different image view in different card layout – Anish Jain Jan 24 '18 at 04:10
  • Or you may use image cache to do so. `imageView.setDrawingCacheEnabled(true);` `Bitmap bitmap = imageView.getDrawingCache();` – Kelly C. Jan 24 '18 at 04:28
0

Set an onClickListener on the ImageView which gets the drawable from the ImageView and sets it as the wallpaper

holder.imageView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        try {
          Bitmap bitmap = ((BitmapDrawable)((ImageView)view).getDrawable()).getBitmap();
          WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
          wallpaperManager.setBitmap(bitmap);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

Make sure to add the wallpaper permissions to your manifest and on runtime

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Napster
  • 1,353
  • 1
  • 9
  • 11