2

I'm working on an app that has a joystick on the MainActivity. The joystick is a custom view and has a Joystick class. When the user touches the joystick and moves it, I want the Y-Axis value to be sent to the MainActivity so that I can work with the value.

In the Joystick.java, I have an onTouch() listener. When touched, the joystick produces the coordinates (the X and Y of the joystick) so that I can get a value from -100 to 100 on the Y-Axis.

public class Joystick extends View implements OnTouchListener{
    int xAxis, yAxis;

    public int getYAxis(){
        return yAxis;
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event){
        // Code to process event and assign xAxis and yAxis values (-100, 100)
    }
}

In the MainActivity I tried to do the following to get the yAxis value:

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

    // ... other code

    Joystick joy = (Joystick) findViewById(R.id.joystick1);
    joy.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            // updateMotorControl() is the funcion I need to pass my value to
            updateMotorControl(joy.getYAxis(), 0);

            return true;
        }
    }

But of course AndroidStudio tells me that joy needs to be declared final. The problem with that is that if it's final, I can never update the value.

I've tried a million things, and I keep getting similar errors. I'm not asking why I get that error. What I want to know is what's the best way to pass that value to the MainActivity and in turn to updateMotorControl()?

Kyle Morgan
  • 660
  • 1
  • 11
  • 21

3 Answers3

0

you should create a new interface

public interface DataCallback {
        /**
         * This method is called when xAxis and yAxis change.
         * @param instance
         */
        void dataIsAvailableNow(int x, int y);
    }

in the Joystick add a DataCallback field:

public class Joystick extends View implements OnTouchListener{
        int xAxis, yAxis;
        DataCallback mCallback;

        public int getYAxis(){
            return yAxis;
        }

        public void setCallback(DataCallback callback) {
            this.mCallback = callback;
        }

        @Override
        public boolean onTouch(View arg0, MotionEvent event){
            // Code to process event and assign xAxis and yAxis values (-100, 100)
            if (mCallback != null) {
                mCallback.dataIsAvailableNow(-100,100);
            }
        }
    }

and in the MainActivity implement the interface and set the listener

joy.setCallback(this);
Lesther Vega
  • 528
  • 3
  • 14
0

Go ahead and make joy final. 'final' doesn't mean you can't change the values inside your object; it only means you can't replace the object itself. Recall that Java passes around objects as their reference value - making joystick final just means you can't replace it with another instance.

Eric S.
  • 1,502
  • 13
  • 31
  • But when I read the values from `updateMotorControl()` they stay at 0 even when I move the joystick – Kyle Morgan May 05 '15 at 20:33
  • Are you intercepting the touch event twice? It looks actually like you're replacing whatver code you had in " // Code to process event and assign xAxis and yAxis values (-100, 100) ". – Eric S. May 05 '15 at 20:35
0

You could implement a callback function.

See here: Callback functions in Java and here: How do I perform a JAVA callback between classes?

Community
  • 1
  • 1
JamesSugrue
  • 14,891
  • 10
  • 61
  • 93