Search code examples
androidandroid-custom-viewandroid-relativelayout

Add click handler to custom Layout


Good day everyone.

I'm inflating a custom layout as rows for objects.

Everything works fine, except I can't manage to set Click/LongClick to the row.

Here's what I've done.

    Layout_BatchRow batchRow = new Layout_BatchRow(this);
    batchRow.New(batchObject);
    batchRow.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            . . .
        }
    });
    batchRow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            . . .
        }
    });

Following is the custom layout class:

public class Layout_BatchRow extends RelativeLayout {
    public Layout_BatchRow(Context context) {
        super(context);
        inflate(getContext(), R.layout.batch_row, this);
        . . .
    }
}

And here is the layout XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"
    tool:context=".Layout_BatchRow">

    . . .

</RelativeLayout>

The listeners are not reached while debugging.

Any idea?


Solution

  • I think you don't need to add this :

    android:clickable="true"
    android:focusable="true"
    android:longClickable="true"