Home > Net >  onclick event not working on cardview? Java xml
onclick event not working on cardview? Java xml

Time:04-07

I am new to android development and I am trying to put an onclick listener to the cardview here is my xml code:

    <androidx.cardview.widget.CardView
        android:id="@ id/makecards"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        app:cardCornerRadius="12dp"
        android:layout_margin="12dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp"
            android:gravity="center">

            <ImageView
                android:src="@drawable/credit"
                android:layout_width="80dp"
                android:layout_height="80dp">
            </ImageView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="Make Cards"
                android:textColor="@color/black"
                android:textSize="18sp"
                ></TextView>

        </LinearLayout>


    </androidx.cardview.widget.CardView>

and in my java file i am using id given to the card view but nothing is getting printed :

 public class containerActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.containerscreen);

    CardView card_view = (CardView) findViewById(R.id.makecards);
    card_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Cliked on make cards");
          

        }
    });

}

}

it must give the print the line but nothing is hapening?

CodePudding user response:

Try putting on Click on the linear layout inside your Card view, because the linear layout has height and width set as match parent so it is covering the whole Card view, that's why when you are clicking it is not working.

  • Related