How to find the whole X and Y coordinates of the text?
How to find the whole X and Y coordinates of the text?
I'm developing a kids alphabets learning application. I need to get the X and Y coordinates of the whole text area from a textview to allow them to draw over the text only. For Example: I have a letter A in my text view, I need to allow the user to draw the letter only over the A text only and need to block the drawing outside the text.
My only problem is I don't know how to find the text area for alphabets as I have increased the font size for the text to display bigger and center aligned it in the screen.
Any suggestions will be really helpful.
2 Answers
2
Something like this (https://www.youtube.com/watch?v=15B3L78jWfI)?
To stop user from drawing outside of the given area, overriding (12:22) onTouchEvent() you could just put a clause checking whether the given coordinates are inside, or not.
To check if they are inside, you could write such a simple function:
int MIN_X;
int MAX_X;
int MIN_Y;
int MAX_Y;
boolean isInside(int xCord, int yCord)
if(xCord>=MIN_X && xCord<=MAX_X && yCord>=MIN_Y && yCord<=MAX_Y)
return true;
return false;
You'd only have to override the min and max values, each time the chosen area changes.
Thanks for your reply. I too have the same idea to avoid drawing outside the text area but how can I identify whether the user touched x and y coordinates are inside the text area or not?
– Arvind
Sep 10 '18 at 7:24
@Arvind I've edited the reply.
– Komiii
Sep 10 '18 at 7:37
I couldn't find any methods from textview to get the min x, miny, max x,max y? Do you have any idea ?
– Arvind
Sep 10 '18 at 7:52
Use textView.getX(), getY(), getWidth() and getHeight() to determine bounds.
– Komiii
Sep 10 '18 at 8:12
Sorry. This is not working. It allows me to draw below the text area.
– Arvind
Sep 10 '18 at 10:43
Try the below mentioned code:
ViewTreeObserver vto = textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Initialize a new integer array two hold the TextView x y position
int location = new int[2];
textView.getLocationOnScreen(location);
);
Thanks for your reply. Already I was able to get the x and y coordinates. My only problem is how to identify the whole text area to compare it with the x and y coordinates of the user touched area ?
– Arvind
Sep 10 '18 at 11:57
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Using PorterDuff might help to clear and show based on content of canvas.
– Raviprakash
Sep 10 '18 at 7:18