znq
znq

Reputation: 44973

Android: InputConnection is missing a getSelection() method

Why do I have a setSelection in InputConnection, but not a getSelection()?

Should I just do a getTextBeforeCursor(VERY_HIGH_NUMBER, 0) and calculate the .length() of that string?

Upvotes: 5

Views: 1090

Answers (3)

Jimmy
Jimmy

Reputation: 168

I had this problem. I wanted to refactor out references to my EditText and use the InputConnection only. To get the selection start and end, I ended up using getExtractedText(…) method like so:

    private static final ExtractedTextRequest request = new ExtractedTextRequest();
    private InputConnection ic;

// …

    ExtractedText extractedText = ic.getExtractedText(request, 0);
    int start = extractedText.selectionStart;
    int end = extractedText.selectionEnd;

Upvotes: 0

CP Taylor
CP Taylor

Reputation: 119

I agree, it's silly that getSelection() doesn't exist. Your solution works ok but you have to assume that there's just a cursor showing and not a whole selected range of text. I haven't yet figured out how to fill that hole.

EDIT: Oh, of course:

int selStart = ic.getTextBeforeCursor(HIGH_NUMBER, 0).length();
String sel = ic.getSelectedText(); 
int selEnd = selStart + (sel==null? 0: sel.length());

Upvotes: 2

Related Questions