Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a click listener to a CardView?

Tags:

java

android

xml

I'm new to android development and fairly new to coding in general, and I'm trying to make it so that when I click on one of my cards, it will display a toast. A different one for each card. I've looked everywhere and nothing works.

This is one of my cards. All of my cards are contained within a RelativeLayout

    <FrameLayout
        android:id="@+id/frame_orange"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ripple"
        android:foreground="?android:attr/selectableItemBackground">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="118dp"
            android:layout_height="123dp"
            android:layout_gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/pi" />

    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|bottom"
        android:paddingBottom="10dp"
        android:text="Definition"
        android:textColor="@android:color/black"
        android:textSize="25dp" />
</android.support.v7.widget.CardView>

This is my what I've added to my MainActivity.java

public static class PlaceHolderFragment extends Fragment implements View.OnClickListener{

View rootView;

public PlaceHolderFragment(){

}

public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_main, container, false);
    return rootView;
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.card_definition:
            Toast.makeText(getActivity().getApplication(), "Test",
                    Toast.LENGTH_LONG).show();
    }
}
}

I'm not sure If I'm doing this completely wrong but I would appreciate help.

Edit: the entirety of my MainActivity.java

package com.piplex.kylefoster.piplex;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class MainActivity extends Activity {

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

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceHolderFragment extends Fragment {

    View rootView;

    public PlaceHolderFragment() {

    }

    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_main, container, false);
        CardView cardView = (CardView) findViewById(R.id.card_definition);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplication(), "Test",
                        Toast.LENGTH_LONG).show();
            }
        });
        return rootView;

    }
}
}
like image 778
Kyle Foster Avatar asked Jan 24 '26 00:01

Kyle Foster


1 Answers

You can easily do it with adding this code to public View OnCreateView :

    CardView cardView = (CardView) findViewById(R.id.card_view);

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //write your function here.
        }
    });
like image 84
Ali Motameni Avatar answered Jan 26 '26 12:01

Ali Motameni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!