Cris
Cris

Reputation: 2021

Set onClickListener on the RelativeLayout contained by CardView

I am trying to set the onClickListener on each of my CardView elements. Since I was not able to set the onClickListener on the cards themselves (see here), I thought about setting it to the RelativeLayout contained by the cards. Right now I am using the native CardView library and applied this tutorial.

Here's my MainActivity's onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recycleCardView = (RecyclerView) findViewById(R.id.cardList);
    recycleCardView.setHasFixedSize(true);

    LinearLayoutManager llmanager = new LinearLayoutManager(this);
    llmanager.setOrientation(LinearLayoutManager.VERTICAL);
    recycleCardView.setLayoutManager(llmanager);

    data = createList(9);

    ContactAdapter ctadapter = new ContactAdapter(data);
    //...
    recycleCardView.setAdapter(ctadapter);

this is the item_card_view xml:

 <?xml version="1.0" encoding="utf-8"?> 
 <android.support.v7.widget.CardView
       xmlns:card_view="http://schemas.android.com/apk/res-auto"
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/card_view"
       android:layout_width="match_parent" android:layout_height="match_parent"
       card_view:cardCornerRadius="4dp"
       android:layout_margin="5dp">

    <RelativeLayout android:id="@+id/nativecardlayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:clickable="true"
       android:background="?android:selectableItemBackground">        

      <!--some more layout-->

    </RelativeLayout>
 </android.support.v7.widget.CardView>

here's my ContactAdapter's onCreateViewHolder:

@Override
public ContactViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext())
                            .inflate(R.layout.item_card_view, viewGroup, false);

    RelativeLayout cardlayout = (RelativeLayout)
              viewGroup.findViewById(R.id.nativecardlayout); //NullPointerException here
    cardlayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(viewGroup.getContext(), "Clickable card", Toast.LENGTH_LONG).show();
        }
    });
    return new ContactViewHolder(itemView);
}

My problem is that the RelativeLayout that I am trying to load, which is inside the cardview element, is null. Therefore I get a NullPointerException.

How can I set onClickListener on each RelativeLayout? Is this possible?

Upvotes: 1

Views: 881

Answers (2)

Willi Ye
Willi Ye

Reputation: 44

You can set a listener to all Views. So it's possibile.
replace this

RelativeLayout cardlayout = (RelativeLayout)
          viewGroup.findViewById(R.id.nativecardlayout);

with this

RelativeLayout cardlayout = (RelativeLayout)
          itemView.findViewById(R.id.nativecardlayout);

Upvotes: 0

Harin
Harin

Reputation: 2423

See below edit, its logical mistake:

RelativeLayout cardlayout = (RelativeLayout)itemView.findViewById(R.id.nativecardlayout);

Upvotes: 2

Related Questions