I am working on soft keyboard where I need to set the cursor position in IME edit text.
As shown in above image, I had created soft keyboard, as we can see some text is entered in edit text and current cursor position(shown by blue indicator).
I need to set the cursor position at the end of the current line (in our case at the end of the line first shown by red color in image)
I have tried with different functions provided with InputConnection
, I tried with,
CharSequence seq = conn.getTextBeforeCursor(1000, 0); // will get as much characters as possible on the left of cursor
And one more thing, I also need to count to the number of lines in edit text (in our case it's two).
I'm sorry to say you wont be able to do that from a IME. For that purpose you should access some methods of the EditText, but there isn't there any possible API available, because the textview control is in an activity another process, not your process.
All the information you can get is what you have in the EditorInfo parameter that you can see in some InputMethodService methods, such as onStartInput(EditorInfo attribute, boolean restarting). Probably in the future the include some other way of accessing more info, but at this point, everything you can do os what you are asking for, is to change the cursor position with the
setSelection
method ofInputConnection
, but you also don't know which is the position of the end of the first line, so that wont be really useful for what you need.Some of the other answers seem overly complicated or incomplete. This is a general answer for future visitors.
In an activity
If you have a reference to EditText then it is easy.
Set the cursor position
Set a selected range
In an IME (keyboard)
It's a little more difficult from within an IME because you don't have direct access to the EditText. However, you can use the InputConnection to set the cursor position and selection.
The following answers get the input connection like this from within your
InputMethodService
subclass:Set the cursor position
Set a selected range
Move the cursor to the start
Move the cursor to the end
This method isn't guaranteed to work because the extracted text will not be be the entire text from the EditText if the text is very long. However, for most situations it is fine. Another option would be to use a combination of the following
inputConnection.getTextBeforeCursor(numberOfChars, 0)
inputConnection.getSelectedText(0)
inputConnection.getTextAfterCursor(numberOfChars, 0)
where
numberOfChars
is a large number.Get current cursor index (or selection)
In the case that the
extractedText
does not return the full text of theEditText
, thestartOffset
tells you from what point it was pulled from. Then you can get the actual cursor index by adding thestartOffset
to the extracted text's selection start or end.Move the cursor relative to its current position
Once you know the current position of the cursor, it is easy to move it around. Here is an example that moves the cursor to the beginning of the previous word.
See other
BreakIterator
options here.You can also move left, right, up and down by sending d-pad down up events.
Move left
Move right
Move up
Move down
Below is some working code to do what you are wanting.
It's quite simple:
To set the cursor position to the end of the first line, we first have to get the index of the last text character of that line. There is a method on the EditTexts' Layout to do this.
The Layout is what the edittext uses internally to layout the text, and it has a method called getLineEnd which returns the text index at the end of a line (we need to subtract 1 from this, otherwise our cursor would end of up at the start of the next line).
Once we have the position to set the cursor to, we just use the setSelection method of the EditText.
We can also get the number of lines really easily with
The trick with this code, is that it has to be run after the EditText's Layout has been built. That means that if we just put this in our onCreate or onResume, it wouldn't work. The line count would be 0.
So we have to run this code after the layout pass has been completed. Which we do by adding a layout listener to the edittext, and running the code after its complete.
Heres the full activity code: