Custom view longClickListener does not respond

Asked

Viewed 76 times

0

I’m trying to get my view to accept long clicks, but nothing happens.

I looked at other similar posts but found no solution.

Any idea what might be going on?

<?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;

}

}

}
  • returns false..

  • And use Log.v("onLongClick", "Foi clicado");

  • I tried. Unsuccessfully...

  • Tried to put something here: ALGUMA_VIEW.setOnLongClickListener(new MyLongClickListenerClass());

  • For example: btn = (Button) findViewById(R.id.button1);&#xA; btn.setOnClickListener(this);&#xA; btn.setOnLongClickListenersetOnLongClickListener(new MyLongClickListenerClass());;

  • setLongClickable(true); Why set true before clicking?

  • When I think of this kind of problem I always debug and see if the method is being called, ViewForLayout is being executed?

  • try tbm this Toast.makeText(v.getContext(), "I'm in", Toast.LENGTH_SHORT).show();

Show 3 more comments

1 answer

1

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

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

An example of the 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 my implementation I want the largest size available for the View.

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

If I’m not mistaken layout_width and layout_height you have the 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 have no way to test at the moment, I am not completely sure of the above values. Any error I can fix

I recommend looking at this document http://developer.android.com/training/custom-views/custom-drawing.html#layouteevent for more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.