Reputation: 11
I am trying to get touchscreen functionality. I want to perform some actions based on whether user touches the screen up or down.
Here is my code for this
public class Sankhyaki extends Activity implements OnTouchListener, OnKeyListener {
float x = 0, y = 0;
public static final String TAG="Sankhayaki";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sankhyaki);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sankhyaki, menu);
return true;
}
public boolean onTouch(View v, MotionEvent me)
{
x = me.getX();
y = me.getY();
switch ( me.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("\n ACTION DOWN");
Log.i(TAG, "DOWN");
Log.i(TAG,"DOWN");
Log.i(TAG, "x = " + x + "y = " + y );
break;
case MotionEvent.ACTION_UP:
System.out.println("\n ACTION UP");
Log.i(TAG, "UP");
Log.i(TAG, "x = " + x + "y = " + y );
break;
case MotionEvent.ACTION_MOVE:
System.out.println("\n ACTION MOVE");
Log.i(TAG, "MOVE");
Log.i(TAG, "x = " + x + "y = " + y );
break;
}
return false;
}
}
While running it on Emulator, When I click on the screen I dont see any message in the LogCat or Console. I am not sure if I am doing any thing wrong. just because I am not seeing any Log message in Logcat or Console, it looks like control is not going in the switch case.
There is no way I validate whether I am doing it right or not.
I want to put more code in the switch case only after I am sure that control goes in the switch case which is not happening here,
Any information here would be helpful.
Upvotes: 1
Views: 224
Reputation: 1506
You need to properly register touch event handlers on the appropriate views...
http://developer.android.com/guide/topics/ui/ui-events.html
http://developer.android.com/reference/android/view/View.OnTouchListener.html
You will need to set it on the appropriate View item from your layout..
View v = findViewById(R.id.whateverView);
v.setOnTouchListener(new onTouchListener()
{
public void onTouch(View v, MotionEvent event)
{
//handle the event
}
});
Upvotes: 0
Reputation: 6159
You have to register your activity as a listener. First of all, in your XML layout file, you need an id for the root view: android:id="@+id/your_view"
In youer onCreate() method, write this:
View v=findViewById(R.id.your_view);
v.setOnTouchListener(this);
Upvotes: 1