0

I have this activity that detects Multi-Touch and counts each instance that the device is tapped over 3 times. What I want is to do a check in Main to see if it reaches the limit that have been set.

Main Activity

package com.test.multitouch;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
// DETECT more than 20 counts here and display a toast

} 

Custom View which extends view

package com.test.multitouch;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;    
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

@SuppressLint("ClickableViewAccessibility")
public class custom_view extends View {

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

final int MAX_NUMBER_OF_POINT = 4;
float[] x = new float[MAX_NUMBER_OF_POINT];
float[] y = new float[MAX_NUMBER_OF_POINT];
boolean[] touching = new boolean[MAX_NUMBER_OF_POINT];
int count = 0;

long cur = System.currentTimeMillis();
long dur = 30000;
long fut = cur + dur;

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

public custom_view(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public custom_view(Context context) {
    super(context);
    init();
}

void init() {
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(40);
}

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < MAX_NUMBER_OF_POINT; i++) {
        if (touching[i]) {
            switch (i) {
            case 1:
                paint.setColor(Color.BLUE);
                break;
            case 2:
                paint.setColor(Color.RED);
                break;
            case 3:
                paint.setColor(Color.YELLOW);
                break;
            case 4:
                paint.setColor(Color.GREEN);
                break;
            }
            canvas.drawCircle(x[i], y[i], 70f, paint);
        }
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = (event.getAction() & MotionEvent.ACTION_MASK);
    int pointCount = event.getPointerCount();


    if (pointCount > 3) {
        Log.i("LOG", "register multi touch");

        count++;

        if (count > 20) {

            //SEND BACK TO MAIN TO SAY IT HAS BEEN ACHIEVED

        }

    }

    for (int i = 0; i < pointCount; i++) {
        int id = event.getPointerId(i);

        if (id < MAX_NUMBER_OF_POINT) {

            x[id] = (int) event.getX(i);
            y[id] = (int) event.getY(i);

            if ((action == MotionEvent.ACTION_DOWN)
                    || (action == MotionEvent.ACTION_POINTER_DOWN)
                    || (action == MotionEvent.ACTION_MOVE)) {

                touching[id] = true;
            } else {
                touching[id] = false;
            }
        }
    }
    invalidate();
    return true;
}
}
zircon
  • 19
  • 3
  • As the activity reference is passed in to the view as the `Context` parameter, what's stopping you from using that reference to call a method on it and send the desired value? – user Nov 02 '14 at 11:06
  • I'm fairly new to android and I don't understand what you're telling me – zircon Nov 02 '14 at 22:05

1 Answers1

0

Your custom view has a reference to the context it's attached to. I assume this view is running in the same activity you are trying to reference. You can get the reference to the activity as follows:

    MainActivity mainActivity = (MainActivity) custom_view.this.getContext();

I also assumed you are calling this from inside your onTouchListener. You need to reference the "this" to get to the outterclass instance, otherwise this will refer to the instance of your onTouchListener class instead. Also, you should capitalize class names, so you can recognize from the capitalization whether we are referring to an instance or class definition.

Once you have the reference, you can call some function on main Activity to update it.

    mainActivity.callSomeFunction();
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • Do you have any examples I can refer to. I'm having a hard time understanding this – zircon Nov 02 '14 at 22:41
  • See the link, for an example of referencing outter class instance from an inner class. http://stackoverflow.com/questions/56974/keyword-for-the-outer-class-from-an-anonymous-inner-class As I said, your onTouchListener is an inner class, and if you want to call back to mainActivity from inside the inner class, you need to get the outter instance, which has the function getContext(). The outter class is bound to the Activity, and you can get the reference from getContext(). – NameSpace Nov 03 '14 at 00:10