Question

Write a snippet of code that logs or displays the coordinate of the touch event when...

Write a snippet of code that logs or displays the coordinate of the touch event when the finger is pressed on the screen of your App.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code has been written using the onCreate Bundle and it is the easiest way we could do the certain tasks performed below:

Activity Code:

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final TextView tv = (TextView)findViewById(R.id.tv);

// It will listen to all the touch activities

final View touch = findViewById(R.id.touch);

touch.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent ev) {

//Returning the touched co-ordinates

tv.setText("Co-ordinates of Area Touched : " +String.valueOf(ev.getX()) + "X" + String.valueOf(ev.getY()));

return true;

}

});

}

Layout Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android:id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/touch"
android:background="#fff"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Getting Co-ordinates"
/>
</LinearLayout>

Please rate the answer if it helped.....Thankyou

Hope it helps....

Add a comment
Know the answer?
Add Answer to:
Write a snippet of code that logs or displays the coordinate of the touch event when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT