2

I am using this library in my app: Card Library : Guide and created a card as shown in the example. Now I need to handle a click event, but I receive an error when instantiating the following: new Card.OnCardClickListener, which says OnCardClickListener is abstract: cannot be instantiated. In my Activity I have this code:

    Card card = new Card(getBaseContext());//getContext()
    CardHeader header = new CardHeader(getBaseContext());
    card.addCardHeader(header);
    final CardViewNative cardView = (CardViewNative) findViewById(R.id.carddemo);
    cardView.setCard(card);

    cardView.setClickable(true);
    cardView.setOnClickListener(new Card.OnCardClickListener()); //error here

What's wrong? How can I set my card as clickable and then handle click/touch events?

EDIT

If I use it this way:

cardView.setOnClickListener(new Card.OnCardClickListener(){//error here
        @Override
        public void onClick(Card card, View view) {
            // This will execute when the the card is clicked
        }
    });

it says cannot resolve method setOnClickListener(it.gmariotti.cardslib.library.internal.Card.OnCardClickListener)

EDIT1

You guys are right, I set the setOnClickListener on the card itself, and there's no error anymore, but still it doesn't perform any action when the card is clicked. That's the new code:

     //Create a Card
    Card card = new Card(this);//getContext()

    //Create a CardHeader
    CardHeader header = new CardHeader(this);
    //....
    //Add Header to card
    card.addCardHeader(header);

    //Set card in the cardView
    CardViewNative cardView = (CardViewNative) this.findViewById(R.id.carddemo);
    cardView.setCard(card);

    card.setClickable(true);
    card.setOnClickListener(new Card.OnCardClickListener(){
        @Override
        public void onClick(Card card, View view) {
            // This will execute when the the card is clicked
            Toast.makeText(CardMariotti.this,"Clickable card", Toast.LENGTH_LONG).show();
        }
    });

I performed a debug, but the onClick method is never called. How come?

Cris
  • 2,002
  • 4
  • 30
  • 51

3 Answers3

3

Card.OnCardClickListener is an Interface. You must create the class that defines how the functions within the interface are to be used. Try this:

card.setOnClickListener(new Card.OnCardClickListener() {
    @Override
    public void onClick(Card card, View view) {
        // This will execute when the the card is clicked
    }
});

Source for answer: https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARD.md#clickable-card

Information on Java interfaces: Java Interfaces?

EDIT

Furthermore, remove android:clickable="true" from the RelativeLayout contained by the card. It's stealing the click for itself.

Community
  • 1
  • 1
BVB
  • 5,380
  • 8
  • 41
  • 62
2

I've found the error but I don't know how to handle it. It's in the layout containing the card, cardslib_item_card.xml. If I remove everything that I've added and I just keep this:

<it.gmariotti.cardslib.library.view.CardViewNative     xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    card:card_layout_resourceID="@layout/native_card_layout"
    style="@style/native_recyclerview_card.base"
    card_view:cardCornerRadius="4dp"
    android:id="@+id/carddemo"
    android:layout_marginTop="12dp"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_marginLeft="12dp" android:layout_marginRight="12dp">

then the card gets clicked, but if try to personalize it, for example adding a Relative Layout, as such:

<it.gmariotti.cardslib.library.view.CardViewNative     
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    card:card_layout_resourceID="@layout/native_card_layout"
    style="@style/native_recyclerview_card.base"
    card_view:cardCornerRadius="4dp"
    android:id="@+id/carddemo"
    android:layout_marginTop="12dp"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_marginLeft="12dp" android:layout_marginRight="12dp">

    <RelativeLayout android:id="@+id/cardlayout"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:background="?android:selectableItemBackground"              
        android:clickable="true">
    <!-- layout containing 3 TextViews -->
    </RelativeLayout>
</it.gmariotti.cardslib.library.view.CardViewNative>

then the card doesn't get clicked anymore. To be more precise, it gets clicked just sometimes, as if the RelativeLayout is covering the whole card and and when i find a spot where it's not then it detects my finger. It works if I only add an empty card, but how am I supposed to customize it then?

It's like having two views on top of each other

Cris
  • 2,002
  • 4
  • 30
  • 51
  • 1
    Remove android:clickable="true" from the RelativeLayout. It's stealing the click for itself. – BVB Apr 30 '15 at 17:37
  • Your last comment worked! you should consider editing your last answer so I will accept it ;) – Cris May 02 '15 at 12:11
1

You have to define the clickListener on the card (model) instead of CardView (view)

//Set onClick listener
card.setOnClickListener(new Card.OnCardClickListener() {
            @Override
            public void onClick(Card card, View view) {
                Toast.makeText(getActivity(),"Clickable card", Toast.LENGTH_LONG).show();
            }
        });

At the same time you should use:

   //it is not necessary if you define a ClickListener on the card
   card.setClickable(true);  

Btw, you should also use something like this in an Activity:

Card card = new Card(this);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Ok there's no error now but still the method doesn't perform any action. See my EDIT1. – – Cris Apr 28 '15 at 18:20
  • ok, I've run the debug but no, the `onClick` seems to be never called.... -Basically, when I press the card nothing seems to happen... why? do I need to post other details? – Cris Apr 29 '15 at 01:57
  • I've also changed the `getBaseContext()` method and used `Card card = new Card(this);` instead, as shown in EDIT1. But still doesn't make the `onClick` method to be called. – Cris Apr 29 '15 at 15:26
  • To solve it I've tried a workaround, creating a `RelativeLayout` object referencing to the RelativeLayout view inside the `cardview` in the xml file, and setting the `onCardClickListener` on the RelativeLayout object: it's working this way, but I don't think that this was how the `cardslib`` is supposed to be used! – Cris Apr 29 '15 at 15:27
  • @CrisBenois of course you can use library without this workaround. I don't know what it is your issue – Gabriele Mariotti Apr 29 '15 at 16:58
  • @GabrieleMariotti can you please view this issue : http://stackoverflow.com/questions/32297099/cardlib-topcoloredcard-setupinnerelements-is-not-called – KOTIOS Aug 30 '15 at 13:40