Android ImageView's onClickListener does not work

前端 未结 15 1797
情话喂你
情话喂你 2020-12-01 13:30

I have an ImageView for which I wanted to implement the onClickListener. But when I click on the image, nothing happens. Event the Logcat does not show any errors.

F

15条回答
  •  遥遥无期
    2020-12-01 14:24

    Try by passing the context instead of the application context (You can also add a log statement to check if the onClick method is ever run) :

    imgFavorite.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("== My activity ===","OnClick is called");
                Toast.makeText(v.getContext(), // <- Line changed
                        "The favorite list would appear on clicking this icon",
                        Toast.LENGTH_LONG).show();
            }
        });
    

提交回复
热议问题