CardView does not indicate it is being clicked

165 Views Asked by At

I have a layout with 4 cards arranged vertically, and what I would like is t have a click listener for each. Below is my entire layout:

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="100"
    android:background="#bbbbb5"
    xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout">


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView"
    android:layout_alignRight="@+id/imageView"
    android:layout_alignEnd="@+id/imageView"
    android:src="@drawable/ic_navigate_next_white_48dp"
    android:layout_marginBottom="40dp" />

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view1"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    card_view:cardCornerRadius="4dp"
    android:layout_marginTop="280dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:onClick="card1ClickMethod">

    <TextView
        android:id="@+id/choice_text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view2"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    card_view:cardCornerRadius="4dp"
    android:layout_marginTop="340dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:onClick="card2ClickMethod">

    <TextView
        android:id="@+id/choice_text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view3"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    card_view:cardCornerRadius="4dp"
    android:layout_marginTop="400dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:onClick="card3ClickMethod">

    <TextView
        android:id="@+id/choice_text3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view4"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    card_view:cardCornerRadius="4dp"
    android:layout_marginTop="460dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:clickable="true"
    android:onClick="card4ClickMethod">

    <TextView
        android:id="@+id/choice_text4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:textColor="#212121" />
</android.support.v7.widget.CardView>

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:id="@+id/imageView"
        android:layout_gravity="center_vertical|top"
        android:src="@drawable/plane1"
        android:layout_weight="0"
    android:layout_marginBottom="50dp"
    android:layout_toRightOf="@+id/prem_BACK"
    android:layout_marginTop="-65dp"
    android:clickable="true" />


<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resulttext3"
        android:layout_gravity="center_vertical|left" android:textSize="15dp"
    android:layout_above="@+id/progressBar1"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="5dp" />
<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar1"
        android:layout_gravity="right|center_vertical"
        android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"/>
<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/prem_BACK"  android:layout_marginLeft="0dp"
         android:layout_marginTop="0dp" android:visibility="gone"
        android:src="@drawable/background4"/>

The issue I have is in my activity I simply have a click listener:

card1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v) {
                Log.d("NICK","Card 1 clicked.");
                ...................
        });

and I have this exactly the same for the other 3 cards. When testing I only see confirmation in my logcat that the first card is being clicked. Is there something I am missing to have click listeners for the other 3 cards?

3

There are 3 best solutions below

0
On

The android:OnClick="method" needed to be inside the TextView child of each CardLayout.

0
On

You will need a click listener for each of the button elements you are wanting to monitor. You will need to use a unique listener for each (or at least make sure you set the listener on each view).

2
On

You have not defined an on click attribute for each card. Before you can listen for clicks you must add:

<!-- XML code for each card -->
android:onClick="method"

where method is the name of the method in the appropriate java activity to handle the click. You must have a method in your java corresponding to the onClick method name:

// java code in activity
public void method(View V) {
    // some code on click
}

This may be the easiest way to go about this.