Get Y coordinate of motion touch event android

There is an image exist. I try to press the the top part of the image. The value i get is from motion event:

e.getY() 

The value is not the same as the value I got from getTop();

textview.getTop();

Below is my code:

    public class level1 extends AppCompatActivity {

    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private float mPreviousX;
    private float mPreviousY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Intent intent = getIntent();

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
        String firstWord = dbHandler.getWord(1);
        String[] firstWordArray = firstWord.split("-");
        ArrayList<String[]> finalWord = new ArrayList<>();

        for (int i = 0; i < firstWordArray.length; i++) {
            finalWord.add(firstWordArray[i].split(","));
        }

        TextView textview = (TextView) findViewById(R.id.textView2);

        int x1 = textview.getLeft();
        int y1 = textview.getTop();

        int x2 = textview.getRight();
        int y2 = textview.getBottom();

        float width = x2 - x1;
        float height = y2 - y1;

        Log.i("Height",""+y1);
        Log.i("Height",""+y2);
        Log.i("Height",""+e.getY());
        Log.i("Height",""+textview.getY());
        Log.i("Height",""+textview.getHeight());
        Log.i("Height",""+height);
        Log.i("Width",""+x1);
        Log.i("Width",""+x2);
        Log.i("Width",""+e.getX());
        Log.i("Width",""+textview.getWidth());
        Log.i("Width",""+width);

        final Dialog dialog = new Dialog(level1.this);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Root Word");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        if (finalWord.size() != 0) {
            for (int i = 0; i < finalWord.size(); i++) {
                float x = e.getX();
                float y = e.getY();
                float xVar = 100*(x-x1)/width;
                float yVar = 100*(y-y1)/height;
                if (xVar< (Integer.parseInt(finalWord.get(i)[0]))) {
                    if (yVar < (Integer.parseInt(finalWord.get(i)[1]))) {
                        dialogButton.setText(finalWord.get(i)[2]);
                        break;
                    }
                }
            }
        }
        dialog.show();
        return true;
    }

}

Upvotes: 1

Views: 186

Answers (1)

Rashin
Rashin

Reputation: 796

Maybe this is what you are looking for?

The difference between the values comes from that the MotionEvent's getY() return the event Y coordinate, but the TextView's getTop() method return the Y position relative to its parent.

Let's say your layout is look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me!"
        android:id="@+id/textView"/>

</LinearLayout>

In this case when you click on the TextView the getTop() return 0 because this is the very first (and top) view of its parent. But if you add another TextView to your layout similar to this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me!"
        android:id="@+id/textView"/>

</LinearLayout>

If you now click the TextView with "Click me!" text, getTop() will return non zero value (in my case 38) because the height of the other TextView.

Do you want to value where you touched the image?

Upvotes: 1

Related Questions