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()?

8
  • can you not pass it through a constructor? May 5, 2015 at 20:27
  • You can use this instead of joy (or just write getYAxis()) ;) May 5, 2015 at 20:30
  • @ChrisBolton I can see what you mean, but I'm not sure how that'd work. Could you show me how to do that in code? May 5, 2015 at 20:31
  • "The problem with that is that if it's final, I can never update the value" Not necessarily, you cannot change the reference. But you can change its properties
    – chancea
    May 5, 2015 at 20:31
  • Have you tried making joy final and it didn't work? It shouldn't matter, only the reference cannot change, not the fields.
    – Sassa
    May 5, 2015 at 20:31

3 Answers 3

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);
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.

2
  • But when I read the values from updateMotorControl() they stay at 0 even when I move the joystick May 5, 2015 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 5, 2015 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.