Hamdi
Hamdi

Reputation: 41

ANDROID How do I pass variable between two classes

I have two classes: SingleTouchEventView and Output.

How I can pass the variable eventX from SingleTouchEventView to Output and display it in a textview?

public class SingleTouchEventView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    private final String TAG = "LOG";

    public static Handler handler; 
    private long delay;
    protected boolean hideView = false;

    public void setDelay(long delay) {
        this.delay = delay;
    }

    Handler h2 = new Handler() {
    };
    Runnable run = new Runnable() {

        @Override
        public void run() {
            onTouchUp();
        }
    };
    public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);     
    }
    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                h2.removeCallbacks(run);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                h2.postDelayed(run, delay);
                if(hideView) {
                    return false;
                }
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }
    protected void onTouchUp() {

    }    
    public void clear() {

    }

}

Derived class

public class Output extends SingleTouchEventView

Upvotes: 0

Views: 87

Answers (1)

Knossos
Knossos

Reputation: 16038

Without understanding your core structure deeply, there is one suggestion I can make. Attempt to use an Event bus to transmit data. There are many available, I am fond of GreenRobots EventBus.

You simply post a class containing your required data and any class that is registered can listen for this data.

Upvotes: 1

Related Questions