I'm developing an application with multiple timers based on card design, and I need to add three possible interactions with them:
- One tap to toggle between Start/Pause
- Double tap to reset the timer to its original value
- Long click to remove it
I can't figure out how to add the onClickListener and where to. This is my code:
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private ArrayList<Timer> data;
public CardAdapter(ArrayList<Timer> data) {
this.data = data;
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CardViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_timer, parent, false));
}
@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
Timer timer = data.get(position);
holder.timerName.setText(timer.getName());
holder.timerTime.setText(timer.getTimeDisplay());
}
@Override
public int getItemCount() {
return data.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
TextView timerName;
TextView timerTime;
public CardViewHolder(View itemView) {
super(itemView);
timerName = (TextView) itemView.findViewById(R.id.titleString);
timerTime = (TextView) itemView.findViewById(R.id.timerString);
}
}
}
item_timer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_margin="5dp"
android:orientation="vertical"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/timerString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="@font/space_grotesk"
android:textSize="60sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titleString"
android:layout_width="165dp"
android:layout_height="45dp"
android:layout_marginTop="4dp"
android:fontFamily="@font/space_grotesk"
android:maxLength="35"
android:maxLines="2"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timerString"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>