I have a recyclerview which includes a checkbox. I had implemented click listener to that checkbox . But my current issue is checkbox is working only when I do a long press.
here my code is.
My Recyclerview item.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/cardview_light_background"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="#+id/PROJECT_image"
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_avatar_size"
android:background="#drawable/mirlogo"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/label"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/labelsale"/>
<CheckBox
android:id="#+id/PROJECT_fav"
android:layout_width="30sp"
android:layout_height="30sp"
android:background="#drawable/selector"
android:button="#null"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
My Adapter
public class HomeDataManager extends RecyclerView.Adapter<HomeDataManager.RecyclerViewHolder> {
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView mProjectName;
ImageView mImage;
CheckBox mCheck;
RecyclerViewHolder(View itemView) {
super(itemView);
mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
}
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v);
}
#Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
// get the single element from the main array
final HomeProjects projects = HomeProjects.PROJECTS[i];
// Set the values
viewHolder.mProjectName.setText(projects.get(HomeProjects.Field.NAME));
viewHolder.mImage.setImageResource(projects.geti(HomeProjects.Field.IMAGE));
viewHolder.mCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Snackbar snackbar = Snackbar.make(v, "Item Favorited", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
}
#Override
public int getItemCount() {
return HomeProjects.PROJECTS.length;
}
}
Remove your custom RecyclerClickListener and implement setOnCheckedChangeListener for checkbox in the constructor of RecyclerViewHolder.
itemView.mCheck.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(){
//Implement your code here}});
For handling RecyclerlayoutClicks:
This code should be used in the constructor of RecyclerViewHolder class.
itemView.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//Your Code}});
Related
This question is already answered but not for cardview I guess, I have already gone through most answers and experimented with them still my problem is not solved.
I have a recyclerview with adapter and a cardview now I have added a button in a cardview and I want to change the visibility of this button to invisible when the button is clicked, but when I do so the visibility of other buttons in other cards like at position=9 get affected and becomes invisible but I never clicked the button at 9th card.
I found some solutions like writing the onClick under onBindViewHolder method. I did so but still its not working please help!
Here is the code for cardview
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/shop_cardview"
android:layout_width="match_parent"
android:layout_height="280dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardMaxElevation="5dp"
card_view:contentPadding="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/btnGrey">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/shopVolleyImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#mipmap/pyaa_logo_iii"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/shopImageNameTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="8dp"
android:text="Json Image Name"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Button"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Here is the code for recyclerviewadapter
public class shopRecyclerViewAdapter extends RecyclerView.Adapter<shopRecyclerViewAdapter.ViewHolder> {
Context context;
List<shopDataAdapter> dataAdapters;
ImageLoader imageLoader;
public shopRecyclerViewAdapter(List<shopDataAdapter> getDataAdapter,Context context){
super();
this.dataAdapters=getDataAdapter;
this.context=context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_shop,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(final ViewHolder Viewholder, int position) {
shopDataAdapter dataAdapterOBJ= dataAdapters.get(position);
imageLoader=shopImageAdapter.getInstance(context).getImageLoader();
imageLoader.get(dataAdapterOBJ.getshopImageUrl(),
ImageLoader.getImageListener(
Viewholder.VollyImageView,
R.mipmap.ic_launcher,
android.R.drawable.ic_dialog_alert
)
);
Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getshopImageUrl(),imageLoader);
Viewholder.ImageTitleTV.setText(dataAdapterOBJ.getshopName());
Viewholder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Viewholder.check.setVisibility(View.INVISIBLE);
}
});
}
#Override
public int getItemCount() {
return dataAdapters.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView ImageTitleTV;
public NetworkImageView VollyImageView;
public Button check;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleTV=itemView.findViewById(R.id.shopImageNameTV);
VollyImageView=itemView.findViewById(R.id.shopVolleyImageView);
check=itemView.findViewById(R.id.button);
}
}
}
You can do like this declare global variable clickPosition = -1
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
Viewholder.check.setVisibility(View.VISIBLE);
Viewholder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickPosition = position;
notifyDataSetChanged();
}
});
if(clickPosition == position){
Viewholder.check.setVisibility(View.INVISIBLE);
}
}
Don't forgot to write line Viewholder.check.setVisibility(View.VISIBLE); otherwise it will give strange behavior.
If you can give more explain what particular behavior you want I can give more explanation or exact solution.
in a cardview i have an imageview, and two textview. I'm having problems trying to figurate out how to change the text of one of the textview when someone click on the cardview....is this even posible?
The card view are controlled by a recyclerview....in total I have to around 20 cardview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="130dp"
android:background="#color/transparent"
android:padding="0dp"
android:scaleType="fitCenter"
android:src="#drawable/card1" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="5dp"
android:text="Text Here"
android:textColor="#color/red" />
<TextView
android:id="#+id/product"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="SUV"
android:textColor="#color/grey_font"
android:textSize="11dp" />
Here is the click of the card view, I get the posistion of the cardview, but don't know how to get the textview inside the cardview.
public void onItemClick(int position) {
Log.e(TAG, "Bien: " + position);
}
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
Items currentItem = vReyclerViewList.get(position);
String heading = currentItem.getHeading();
String title = currentItem.getTitlee();
holder.vHeading.setText(heading);
holder.vTitle.setText(title);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(vListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
vListener.onItemClick(position);
}
}
}
});
}
}
You have to override the onBindViewHolder() method then implement as below.
Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Model model = getItem(position);
holder.bind(model);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public final TextView title;
public final TextView product;
public RecyclerViewHolder(View itemView) {
super (itemView);
title = itemView.findViewById(R.id.title);
product = itemView.findViewById(R.id.product);
}
public void bind(final Model model){
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title.setText(model.getText());
}
});
}
}
I want to make my CardView CheckBox behave like a RadioGroup so that whenever I check another Card the previous will be unchecked. I now have successfully create a CardView with checkboxes but I am not sure how to make them behave like I want to.
cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="?android:selectableItemBackground"
android:id="#+id/suggest_time_card_view"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/activity_horizontal_margin"
android:background="#drawable/back"
android:gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:id="#+id/tvSuggestTime"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cbSuggestTime"
android:visibility="invisible"
android:checked="false"
android:padding="#dimen/activity_horizontal_margin"
android:layout_alignBottom="#+id/tvSuggestTime"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
CardViewAdapter:
public class SuggestTimeCardAdapter extends RecyclerView.Adapter<SuggestTimeCardAdapter.ViewHolder> {
private Context context;
private List<SuggestTimeList> suggestTimeLists;
private Integer selected_position = -1;
public SuggestTimeCardAdapter(List<SuggestTimeList> suggestTimeLists, Context context){
super();
this.suggestTimeLists = suggestTimeLists;
this.context = context;
}
#Override
public SuggestTimeCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.suggest_time_card, parent, false);
SuggestTimeCardAdapter.ViewHolder viewHolder = new SuggestTimeCardAdapter.ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(final SuggestTimeCardAdapter.ViewHolder holder, final int position) {
final SuggestTimeList suggestTimeList = suggestTimeLists.get(position);
holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());
holder.cbSuggestTime.setChecked(position==selected_position);
holder.cbSuggestTime.setVisibility(View.VISIBLE);
holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//implement the click
Boolean CheckTime = suggestTimeList.getCheckTime();
String reservationTime = holder.tvSuggestTime.getText().toString();
Integer ItemId = suggestTimeList.getTimeId();
holder.cbSuggestTime.setChecked(true);
selected_position = position;
for(SuggestTimeList s : suggestTimeLists) {
s.setChecked(false);
}
suggestTimeList.setChecked(true);
//notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return suggestTimeLists.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvSuggestTime;
public CheckBox cbSuggestTime;
public CardView suggest_time_card_view;
public ViewHolder(View itemView) {
super(itemView);
tvSuggestTime = (TextView) itemView.findViewById(R.id.tvSuggestTime);
cbSuggestTime = (CheckBox) itemView.findViewById(R.id.cbSuggestTime);
suggest_time_card_view = (CardView) itemView.findViewById(R.id.suggest_time_card_view);
}
}
}
Make checkbox in CardView behave like RadioGroup?
You can modify your model by add a field (to store state of checkbox ) like
boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
And in your onBindViewHolder, you can use this field to set checkbox. like this:
// suggestTimeList is an item of list.
holder.cbSuggestTime.setCheck(suggestTimeList.isSelected());
holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// your code ....
// UnSelected all
for (int i = 0; i < suggestTimeLists.size(); i++) {
suggestTimeLists.get(i).setSelected(false);
}
// Only select card you have clicked
suggestTimeLists.get(position).setSelected(true);
notifyDataSetChanged();
}
});
I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I am using adapter to populate data in RecyclerView, the code for this is :
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
public DataAdapter(Context context, ArrayList<Movie> movies) {
this.movies = movies;
this.context = context;
}
public void addItems(ArrayList<Movie> movies) {
this.movies.addAll(movies);
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.title.setText(movies.get(i).getTitle());
Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
}
#Override
public int getItemCount() {
return movies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title;
private ImageView thumbnail;
public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
view.setOnClickListener(this);
}
// Handles the row being being clicked
#Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
Movie movie = movies.get(position);
// start detail activity
Intent i = new Intent(context, MovieDetail.class);
i.putExtra(Constants.MOVIES_OBJECT, movie);
context.startActivity(i);
}
}
}
}
My problem is click listener is only working on the TextView and not on the image , although I have set click listener on whole view which contains both image and text.Is something wrong with my implementation ?
try setting android:focusableInTouchMode="false" to your imageview.
and remove android:clickable="true" or set it to false
Use viewHolder.itemView like :
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your action
});
This will enable click action on whole RecyclerView item.
Add android:clickable="true" to the root RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
android:clickable="true"
android:focusableInTouchMode="true"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="false"
android:focusableInTouchMode="false"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
public class OffersRecycleViewAdapter extends RecyclerView.Adapter {
private Activity mActivity;
private List<OfferShopModel> offerShopModels = new ArrayList<>();
ArrayList<String> allColors = new ArrayList<String>();
public OffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
this.offerShopModels = offerShopModels;
this.mActivity = activity;
}
// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);
return new ItemViewHolder(v);
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int position) {
itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());
}
#Override
public int getItemCount() {
return offerShopModels.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mLinearLayoutBack;
TextView mOffer;
ItemViewHolder(View itemView) {
super(itemView);
mOffer = (TextView) itemView.findViewById(R.id.offer);
mOffer.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// call click event
}
}
}
remove android:clickable="true" from your ImageView or change it to android:clickable="false"
So I have a CardView which has a RecyclerView in it, and I have set an OnClickListener to the CardView. The problem is I want to be able to click anywhere in the CardView but I can't click it inside the RecyclerView. I can only click the part of the CardView that's not part of the RecyclerView. How do I fix this?
Thanks.
<android.support.v7.widget.CardView
android:id="#+id/phone_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<TextView
android:id="#+id/phone_card_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/phone_numbers"
android:textAppearance="#style/TextAppearance.AppCompat.Caption" />
<android.support.v7.widget.RecyclerView
android:id="#+id/phone_card_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/phone_card_title">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v7.widget.CardView>
In your recycler adapater initialize cardview and setonClicklistener for it
example
Viewholder :
public class ViewHolder extends RecyclerView.ViewHolder {
public final CardView cardView ;
public ViewHolder(View view) {
super(view);
cardView = (CardView) view.findViewById(R.id.carView);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.cardView.setOnClickLister(new View.OnClickListener(){
#Override
public void onClick(View view) {
//your logic here
}
});
}