1

I'm working on a Livewallpaper with a Thread Painting all Objects and Custom Objects extended from View. The Problem is that my custom view doesn't fire on click....

Here are the Code Snippeds:

in my WallpaperService Class the OnTouch ist given to my Painting Thread:

    @Override
    public void onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        painting.doTouchEvent(event);
    }

Then in the constructor of my PaintingThread I'm creating instances of my custom view:

public LiveWallpaperPainting(SurfaceHolder surfaceHolder, Context context) {
    for(int i = 0; i < numberOfObj;i++ ){
        obj.add(new Obj(context,objBitmap, objBitmap2, mCanvasWidth, mCanvasHeight, 32, 32 , 20, 10));
    }

Then in the Constructor of my Obj:

    super(context);
    this.context = context;
    this.setEnabled(true);
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);
    this.setClickable(true);
    this.setOnClickListener(this);

The Class implements OnClickListener.

But when I'm logging the onClick nothing happens....:

@Override
public void onClick(View v) {
    Log.d(TAG, "clicked");
}

I'm getting crazy because I tried so much, but nothing worked... :( Please help me. I think the OnClick is catched before my Obj can react?? But don't know why....

I hope I gave you all details needed...

Yumi

user980175
  • 21
  • 1
  • 4

1 Answers1

4

The following code works. Have you on the Activity a other OnClickListener?

public class CustomView extends View implements OnClickListener {
Paint paint;

public CustomView(Context context) {
    this(context, null);        
}

public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);                
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint = new Paint();
    paint.setColor(Color.BLUE);

    canvas.drawRect(0.f, 0.f, 240.f, 240.f, paint);

    this.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Log.d("CustomView", "Click");       
}}

main.xml

<com.examples.view.CustomView
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
Bobert
  • 226
  • 4
  • 16
  • Actually this doesn't work for me and I don't know why... :...( I don't have any activity because its a LiveWallpaper and no normal activity...So whats going wrong here??? – user980175 Feb 10 '12 at 10:31
  • 2
    Better set the onClickListener() int the constructor – Heigo May 04 '14 at 10:29
  • @Heigo - settings onClickListener in the constructor doesn't fire any event. I wonder why onClick fires event in onDraw but not in the constructor. – Alex Jun 27 '16 at 18:39
  • There is 3 constructors in this class, determine which constructor is used and set the onClickListener() there – Heigo Jun 28 '16 at 07:47