Reputation: 402
i'm trying to add a favourite button using toogleButton that displays a certain icon when unchecked and another icon when checked. I tried this but isn't working. Here is the toggleButton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fav_fragment_title"
android:id="@+id/favButton"
android:layout_weight="0.33"
android:layout_gravity="center"
/>
and this is my recycler adapter.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ClipboardManager myClipboard;
private ClipData myClip;
private Context context;
public List<CardItemModel> cardItems;
public RecyclerAdapter(List<CardItemModel> cardItems){
this.cardItems = cardItems;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView copyButton;
ImageView shareButton;
ToggleButton favButton;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
this.title = (TextView)itemView.findViewById(R.id.card_title);
this.content = (TextView)itemView.findViewById(R.id.card_content);
this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
favButton.setChecked(false);
favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher));
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(cardItems.get(position).title);
holder.content.setText(cardItems.get(position).content);
holder.copyButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
myClip = ClipData.newPlainText("label", holder.content.getText().toString());
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();
}
});
holder.shareButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
}
});
holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked)
favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.ic_launcher));
else
favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_cart));
}
});
}
@Override
public int getItemCount() {
return cardItems.size();
}
}
Also how can I add the favourited recyclerView to another fragment?
Upvotes: 3
Views: 2515
Reputation: 23881
Add a Drawable selector and use it as Background in ToggleButton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fav_fragment_title"
android:id="@+id/favButton"
android:layout_weight="0.33"
android:layout_gravity="center"
android:textOn=""
android:textOff=""
android:background="@drawable/toggle_image"
/>
in res/drawable/toggle_image.xml
:
Add your select/unselect images
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
Upvotes: 4