I've been building controls for the vehicle I want to control. However I'm still quite new to Java and android developing. So I am looking for best practices to handle multiple buttons from the UI. So far I've managed to create 2 buttons which are on the same screen, see code below. Is this a correct way to handle and create buttons?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Left Button */
Button btnLeft = (Button)findViewById(R.id.btnLeft);
btnLeft.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Create thread
case MotionEvent.ACTION_UP:
// End Thread
}
return false;
}
});
/* Right button */
Button btnRight = (Button)findViewById(R.id.btnRight);
btnRight.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// Create thread
case MotionEvent.ACTION_UP:
// End Thread
}
return false;
}
});
}
}
That code actually works - I'm planning to create threads inside the switch-case statements too, I haven't figured out that one yet. Any input would be appreciated.