How to get the position of a selected character or string in EditText

I want to know how I can get the x, y position of a selected character or String in an EditText . Is it possible?

+7
source share
2 answers

Use this code to get the first index of a specific character:

 String s = editText.getText().toString(); int position = s.indexOf("C"); // where C is your character to be searched 
+6
source

Here's how to get the x and y coordinates of a specific character in a TextView, should work for EditText too. offset is the index of the desired character in the text of the view.

 Layout layout = editView.getLayout(); if (layout == null) { // Layout may be null right after change to the view // Do nothing } int lineOfText = layout.getLineForOffset(offset); int xCoordinate = (int) layout.getPrimaryHorizontal(offset); int yCoordinate = layout.getLineTop(lineOfText); 
0
source

Source: https://habr.com/ru/post/891081/


All Articles