0

I have a cardView that has a textView and a Like button. Whenever the like button is pressed it will display that cardView in another fragment, just like favourites.

How can I do this using shared preference or any other method? I've done this to change the color of like button but don't know how to add that cardView to another fragment:

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

      int id = (int)likeImageView.getTag();
      if( id == R.drawable.ic_like){

          likeImageView.setTag(R.drawable.ic_liked);
          likeImageView.setImageResource(R.drawable.ic_liked);

          Toast.makeText(getActivity(),titleTextView.getText()+" added to favourites",Toast.LENGTH_SHORT).show();

      } else {

          likeImageView.setTag(R.drawable.ic_like);
          likeImageView.setImageResource(R.drawable.ic_like);
          Toast.makeText(getActivity(),titleTextView.getText()+" removed from favourites",Toast.LENGTH_SHORT).show();

      }

    }
  });
Shersh
  • 9,019
  • 3
  • 33
  • 61

2 Answers2

0
  1. Create an instance of SharedPreferences in your onclick method

    SharedPreferences prefs = getActivity().getSharedPreferences( "Preference_reference", Context.MODE_PRIVATE);

  2. Use the name of the 'Liked' item as the key and a boolean as the type Alternately you could use a Boolean array as the type if the items never change order.

  3. Save whether or not an item is liked to the shared pref in the onclick.

    if( id == R.drawable.ic_like){

        likeImageView.setTag(R.drawable.ic_liked);
        likeImageView.setImageResource(R.drawable.ic_liked);
    
        prefs.edit().putBoolean("Unique id", true).commit();   
    
        Toast.makeText(getActivity(),titleTextView.getText()+" added to 
        favourites",Toast.LENGTH_SHORT).show();
    
    }else{
    
        likeImageView.setTag(R.drawable.ic_like);
        likeImageView.setImageResource(R.drawable.ic_like);
    
        prefs.edit().putBoolean("Unique id", false).commit();
    
        Toast.makeText(getActivity(),titleTextView.getText()+" removed from favourites",Toast.LENGTH_SHORT).show();
    }
    
  4. In the fragment you want to show items if the are liked, Create another SharedPreferences variable and use it to initialize the fragment so that it shows any item whose key value is set to true.

    if(prefs.getBoolean("Unique Id",false)){Show card}

You can find plenty of resources on how to use SharedPreferences so I kept the code to the bare minimum

Tony
  • 1
  • 4
0
Like_btn.setOnClickListener(new OnClickListener()
{
    **private boolean fun = true;**

    public void onClick(View v)
    {
        if(fun)
        {
            Like_btn.setImageResource(R.drawable.unlike);
            fun=false;
        }
        else
        {
            fun=true;
            Like_btn.setImageResource(R.drawable.like);
            Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
        }
    }
});
JHobern
  • 866
  • 1
  • 13
  • 20