Saturday

Find the co-ordinate of layout in Android

If you really thinking how to starting game development, then it is useful for you.This is simple example of how to find the co-ordinate of layout in Android.This is not a big idea, but more developer want to find their position of finger touch in view.

Here is the some code:
Public class MainActivity extends Activity implements OnTouchListener {
TextView tv;
StringBuilder sb = new StringBuilder();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = new TextView(this);

tv.setText("Drag By One Finger");
tv.setOnTouchListener(this);
setContentView(tv);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
sb.setLength(0);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
sb.append("Down, ");
break;
case MotionEvent.ACTION_MOVE:
sb.append("Move, ");
break;
case MotionEvent.ACTION_CANCEL:
sb.append("Cancel ,");
break;
case MotionEvent.ACTION_UP:
sb.append("Up, ");
break;
}
sb.append(event.getX());
sb.append(",");
sb.append(event.getY());
String test=sb.toString();
Log.d("TAG", "Testing->"+test);
tv.setText(test);
return true;
}
}


Output :

Happy Coding!

0 comments

Posts a comment

Related Posts  www.prandroid.com...
 
© 2011 Android Programming Tutorails | 2012 Templates
Designed by Blog Thiết Kế
Back to top