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