3

I followed the thread answered by Gautier Haroun on how to create a touchscreen and allow tapping and displaying the coordinate. But I want to normalize the coordinate by dividing with the text view width and height respectively to get a range between 0~1.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final TextView textView = (TextView)findViewById(R.id.textView);
        // this is the view on which you will listen for touch events
        final View touchView = findViewById(R.id.touchView);
        final float touchWidth = touchView.getWidth();
        final float touchHeight = touchView.getHeight();

        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {    
                textView.setText("Touch coordinates : " +
                        String.valueOf(event.getX()/touchWidth) + " x " + String.valueOf(event.getY()/touchHeight));
                return true;
            }
        });


        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();
            }
        });
    }

Then the layout:

<TextView
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, TestActivity"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/touchView"
    android:background="#f00"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:alpha="0.5" />

I tried final float touchWidth = touchView.getWidth(); But the result is infinite, so touch Width is 0 instead of around 1500 in my case. Can you tell me how to get the width and height of the View object touch View?

Thanks

2 Answers 2

2

You can use this:

touchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                touchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            else {
                touchView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            final float touchWidth = touchView.getWidth();
            final float touchHeight = touchView.getHeight();
            ...
        }
    });

your code will look like this:

private float touchWidth;
private float touchHeight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final TextView textView = (TextView)findViewById(R.id.textView);
    // this is the view on which you will listen for touch events
    final View touchView = findViewById(R.id.touchView);

    touchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) { 
            if(touchWidth == 0 || touchHeight == 0)
                return false;

            textView.setText("Touch coordinates : " +
                    String.valueOf(event.getX()/touchWidth) + " x " + String.valueOf(event.getY()/touchHeight));
            return true;
        }
    });

    touchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                touchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            else {
                touchView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            touchWidth = touchView.getWidth();
            touchHeight = touchView.getHeight();
        }
    });

    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();
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. But I am not sure how I can return touchWidth and touchHeight and pass them to the touchWidth and touchHeight in onCreate().
simply declare touchWidth and touchHeight variables as global in your class, see my updated answer
0

OnGlobalLayoutListener is one method for getting the measurement

ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
      if (viewTreeObserver.isAlive()) {
      viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
          if (Build.VERSION.SDK_INT < 16) {
               view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
          } else {
               view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
          }
          viewWidth = view.getWidth();
          viewHeight = view.getHeight();
        }
      });
    }

The another method is use onWindowFocusChanged

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  viewWidth = view.getWidth();
  viewHeight = view.getHeight();
 }

1 Comment

be careful that removeOnGlobalLayoutListener(this) works starting SDK 16 (4.1 jelly bean) only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.