简体   繁体   中英

Android OnClick doesn't work with my Cardview

i tried multiple things like android:clickable="true" and setOnClickListener on my Cardview but it doesn't do anything. I tried to get any console outputs or anything else. But it seems that it doesn't really recognizes the click on the Button. I also switched the position of the android:clickable="true" from parent to child -> no difference. has anyone any idea? thanks:)

1. activity_main_menu_1.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/cut_card_background"
    tools:context=".MainMenu">

    <TextView
        android:id="@+id/main_TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IFI03"
        android:textSize="30dp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:layout_margin="12dp"
        />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/main_TextView"
        android:layout_marginTop="20dp"
        android:columnCount="7"
        android:rowCount="7">

        <android.support.v7.widget.CardView
            android:id="@+id/menu_it1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            app:cardBackgroundColor="@color/colorPrimaryAccent"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardUseCompatPadding="true"
            android:clickable="true"
            android:focusable="true"
            android:foreground="?android:selectableItemBackground">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_home" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="IT1"
                    android:textColor="@color/design_default_color_primary_dark"
                    android:textSize="20sp" />


            </LinearLayout>

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

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

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


       </GridLayout>

 
      </RelativeLayout>

2. MainMenu.java

public class MainMenu extends AppCompatActivity {

      CardView it1;
      CardView it2;
      CardView it3;
      CardView it4;
      CardView it5;
      CardView eng;
      CardView pol;
      CardView quiz;



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

        it1 = findViewById(R.id.menu_it1);
        it2 = findViewById(R.id.menu_it2);
        it3 = findViewById(R.id.menu_it3);
        it4 = findViewById(R.id.menu_it4);
        it5 = findViewById(R.id.menu_it5);
        eng = findViewById(R.id.menu_eng);
        pol = findViewById(R.id.menu_pol);
        quiz = findViewById(R.id.menu_quiz);

        it1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        it2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        it3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        it4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        it5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        eng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        pol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });

        quiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Button was Clicked", Toast.LENGTH_LONG).show();
            }
        });
    }

}

In my case it works!

Replace

android.support.v7.widget.CardView

With

androidx.cardview.widget.CardView

Add this dependency:

implementation "androidx.cardview:cardview:1.0.0"

Due to update in Android Support Libraries, android.support.v7 , causing conflicts and no longer working.. https://developer.android.com/topic/libraries/support-library

Just make the following change in your xml:

Change

<android.support.v7.widget.CardView

to

<androidx.cardview.widget.CardView

and also in the Activity file import following:

import androidx.cardview.widget.CardView;

Thats all, Enjoy!

Similar implementation has been answered here.

You can also make setOnClickListener work by setting setOnClickListener to the GridLayout and iterate over the GridLayout view to get the listener to work. This implementation is shown here .

If you are curious about dependencies, this site provides quite a detailed comparison between legacy and androidx libraries, CardView is among them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM