Custom view longClickListener not responding

0

I'm trying to make my view accept long clicks, but nothing happens.

I looked at other similar posts but found no solution.

Any idea what might be happening?

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<com.examples.danilofernandes.housemap.ViewForLayout
android:id="@+id/layout"
android:layout_width="100px"
android:layout_height="100px" />

<Button
android:id="@+id/button"
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout"/>


</RelativeLayout>

The class:

public class ViewForLayout extends View {

...

public ViewForLayout(final Context context, AttributeSet attrs) {
super(context, attrs);

setLongClickable(true);
setOnLongClickListener(new MyLongClickListenerClass());

....

}

public class MyLongClickListenerClass implements OnLongClickListener{

@Override
public boolean onLongClick(View v) {

    Toast.makeText(context, "I'm in", Toast.LENGTH_SHORT).show();

    return true;

}

}

}
    
asked by anonymous 25.01.2015 / 23:36

1 answer

1

Sometimes this problem is because you did not implement the onMeasure method by setting the actual size of your Custom View using the setMeasuredDimension method.

By default the size of the View is equal to the size of the Drawable of the background. With this I believe that Android is not detecting touches in its View and consequently calling the onLongClick method.

An example implementation of onMeasure would be:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = 0, height = 0;

    int suggestedWidth = MeasureSpec.getSize(widthMeasureSpec);
    int suggestedHeight = MeasureSpec.getSize(heightMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    // Not matter, i want the maximum width
    if(Build.VERSION.SDK_INT >= 16) {
        width = Math.max(getMinimumWidth(), suggestedWidth);
    } else {
        width = suggestedWidth;
    }

    if(heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
        if(Build.VERSION.SDK_INT >= 16) {
            height = Math.max(getMinimumHeight(), suggestedHeight);
        } else {
            height = suggestedHeight;
        }
    }

    setMeasuredDimension(width, height);
}

In this my implementation I want the largest size available for View .

Keep in mind that the attributes: layout_width and layout_height influence the value of mode for both height and width.

If I am not mistaken with layout_width and layout_height you have LayoutMode being:

match_parent          -> LayoutMode.AT_MOST (MeasureSpec.getSize retorna o tamanho do pai)
wrap_content          -> LayoutMode.AT_MOST (MeasureSpec.getSize retorna 0)
0dp com layout_weight -> LayoutMode.AT_MOST (MeasureSpec.getSize retorna o tamanho restante para a View)

valor fixo            -> LayoutMode.EXACTLY (MeasureSpec.getSize retorna o tamanho exato)

As I can not test at the moment, I am not completely sure of the above values. Any error I can fix

I recommend looking at this document link for more details.

    
26.01.2015 / 11:50