Home > other >  ImageView setOnClickListener Not Work (Java/Android)
ImageView setOnClickListener Not Work (Java/Android)

Time:12-24

Im having some problem with Click Listener on Image View
I try to log some text if I click on the button
But it not work at all

Here is my code, Hope u can help me:

ChatHeadService.java

ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myTag", "This is my message");
                //close the service and remove the chat head from the window
                stopSelf();
            }
        });

Layout_chat_head.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="65dp"
    android:id="@ id/chat_head_root"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--Profile image for the chat head.-->
    <ImageView
        android:id="@ id/chat_head_profile_iv"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/icon_128"
        tools:ignore="ContentDescription" />

    <!--Close button-->
    <ImageView
        android:id="@ id/close_btn"
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:layout_marginLeft="40dp"
        android:src="@drawable/close"
        tools:ignore="ContentDescription" />
</RelativeLayout>

[EDIT] Here is the image of the layout: enter image description here

P/s: I had read the similar problem in here: Android ImageView's onClickListener does not work

But it seem not work for me.

CodePudding user response:

I faced this same problem. ImageView may be hidden / in background by some other view. Use some elevation
android:elevation="3dp"

CodePudding user response:

It simple than i thought

I just change from this:

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

to this:

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, //<-- Right here :)
                PixelFormat.TRANSLUCENT);
  • Related