I'm working on a quotes app.. which contains famous quotes and person names in card view(recycler view).. each cardview contain a checkbox.. what i want to do is.. when ever the user click on a checkbox of a particular quotes card.. i need to display a toast.. saved to favorites..and change the checkbox background to another image(newimg).. when user again clicks on the checkbox the toast is to be displayed.. as removed form favorites ... and the checkbox background image should be default.. thus how to display all the favorites quotes marked by user in a separate activity.. i'm new to android .. I didn't found any references for my purpose..
MainActivity.java
public class MainActivity extends AppCompatActivity {
//recyclerview objects
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] AuthorNames = new String[]{"Navagio Beach", "Anse Source d'Argent Beach", "As Catedrais Beach",
"La Concha Beach", "Bondi Beach", "Nissi Beach"};
String[] QuotesGuide = new String[]{"https://www.tripadvisor.com.my/Attraction_Review-g7777607-" +
"d671779-Reviews-Navagio_Beach_Shipwreck_Beach-Anafonitria_Zakynthos_Ionian_Islands.html",
"https://www.tripadvisor.com.my/Attraction_Review-g477968-d637885-Reviews-Anse_Source_D_Argent" +
"-La_Digue_Island.html",
"https://www.tripadvisor.com.my/Attraction_Review-g609028-d1547522-Reviews-As_Catedrais_Beach-Ribadeo_" +
"Province_of_Lugo_Galicia.html",
"https://www.tripadvisor.com.my/Attraction_Review-g187457-d675885-Reviews-La_Concha_Beach-San_Sebastian" +
"_Donostia_Province_of_Guipuzcoa_Basque_Country.html",
"https://www.tripadvisor.com.my/Attraction_Review-g255060-d257354-Reviews-Bondi_Beach-Sydney_" +
"New_South_Wales.html",
"https://www.tripadvisor.com.my/Attraction_Review-g262055-d1519581-Reviews-Nissi_Beach-Ayia_" +
"Napa_Famagusta_District.html"};
RecyclerView myrv = findViewById(R.id.recyclerView);
MyRecycleViewAdapter myAdapter = new MyRecycleViewAdapter( AuthorNames , QuotesGuide , MainActivity.this);
myrv.setLayoutManager(new LinearLayoutManager(this));
myrv.setAdapter(myAdapter);
}}
MyQuote.java
public class MyQuote {
private String author;
private String quotedesc;
private int isLiked = 0;
//constructor initializing values
public MyQuote(String author, String quotedesc) {
this.quotedesc = quotedesc;
this.author = author;
}
//getters
public String getAuthor() {
return author;
}
public int getIsLiked(){return isLiked;}
public String getQuotedesc() {
return quotedesc;
}
public void setIsLiked(int isLiked) {
this.isLiked = isLiked;
}
}
MyReclerViewadapter.java
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>{
private MyQuote myQuote;
private String[] AuthorNames;
private String[] QuotesGuide;
private Context mCtx;
public MyRecycleViewAdapter(String[] authorNames, String[] quotesGuide, Context mCtx) {
AuthorNames = authorNames;
QuotesGuide = quotesGuide;
this.mCtx = mCtx;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sample_quotecards, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, final int position) {
myholder.tv_author.setText(AuthorNames[position]);
myholder.tv_quote.setText(QuotesGuide[position]);
myholder.im_favlike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//Show "Saved to favourite" toast
Toast.makeText(mCtx, " quote saved to favorites",
Toast.LENGTH_LONG).show();
} else {
//Show "Removed from favourite"
Toast.makeText(mCtx, " quote removed from favorites",
Toast.LENGTH_LONG).show();
}
}
});
// share button of a recycler cardview
myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "share this quote"
+ AuthorNames[myholder.getAdapterPosition()] +
"\nHere is the link to the full review: " + QuotesGuide[myholder.
getAdapterPosition()]);
intent.setType("text/plain");
mCtx.startActivity(Intent.createChooser(intent, "share this quote"));
}
});
}
@Override
public int getItemCount() {
return AuthorNames.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tv_author;
public CheckBox im_favlike;
public TextView tv_quote;
public ImageButton buttonViewOption;
public ViewHolder(View itemView) {
super(itemView);
im_favlike = itemView.findViewById(R.id.likeimg);
tv_author= itemView.findViewById(R.id.author_title);
tv_quote= itemView.findViewById(R.id.quote_text);
buttonViewOption = itemView.findViewById(R.id.imageViewOptions);
}
}
}
what i want to do is :
whenever i click the (chekbox)favorite..the checkbox image is changing..and on back click it comes to default(unchecked)..and it works fine.. but the problem..is i dont understand how to save these checkbox values...when the user exits the app and open the app again i need to maintain the favorites..checkbox values..
how to store all the favorites(quotes cards)... marked(checked) by the user in a separate activity..
i am new to android.. i dont know about shared preferences..can anyone explain in detail step wise ..what should i do.. it helps me lot..