Android joystick event code - Java example

Example android java code for joystick event,
public class MainActivity extends Activity { private TextView t1; boolean continua =false; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView mWebView = (WebView) findViewById(R.id.webView1); mWebView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); WebChromeClient client= new WebChromeClient(); mWebView.setWebChromeClient(client); mWebView.setClickable(false); mWebView.getSettings().setSupportZoom(false); mWebView.getSettings().setJavaScriptEnabled(false); mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); mWebView.getSettings().setAllowFileAccess(true); //mi indirizza alla pagina js contenente lo streaming della ip camera final String url = "http://products.indiacitys.com"; mWebView.loadUrl(url); t1 = (TextView) findViewById(R.id.textView1); } // fine del Thread Inizia public boolean onKeyDown(int keyCode, KeyEvent event) { WebView mWebView = (WebView) findViewById(R.id.webView1); mWebView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); switch(keyCode){ case KeyEvent.KEYCODE_DPAD_UP: t1.setText("SU"); return false; case KeyEvent.KEYCODE_DPAD_DOWN: t1.setText("GIU"); return false; case KeyEvent.KEYCODE_BUTTON_L1: t1.setText("SINISTRA"); return false; case KeyEvent.KEYCODE_DPAD_LEFT: t1.setText("SINISTRA"); return false; case KeyEvent.KEYCODE_BUTTON_R1: t1.setText("DESTRA"); return false; case KeyEvent.KEYCODE_DPAD_RIGHT: t1.setText("DESTRA"); return false; } return super.onKeyDown(keyCode, event); } public boolean onKeyUp(int keyCode, KeyEvent event) { switch(keyCode){ case KeyEvent.KEYCODE_BUTTON_R1: t1.setText("DESTRA RILASCIATO"); return false; case KeyEvent.KEYCODE_BUTTON_L1: t1.setText("SINISTRA RILASCIATO"); return false; case KeyEvent.KEYCODE_BUTTON_THUMBR: t1.setText("SU RILASCIATO"); return false; case KeyEvent.KEYCODE_BUTTON_THUMBL: t1.setText("GIU RILASCIATO"); return false; } return super.onKeyUp(keyCode, event); }; }

Joystick is used for gaming purpose and available in various types in today’s market like in the form of USB or Wi-Fi enables or Bluetooth enabled. But to purchase these type of joystick, you have to make extra payment. But with the help of Android standard input method, we have successfully able to make our smart phone as Joystick. Now, using your android based phone, through the help of Wi-Fi or Bluetooth, it will act as a gamepad and play your game from specific distance using your phone.

I have attached example for Joystick event code,
Sign In or Register to comment.