The question is long ago. But I want to help people who arrive here. Here is what I got after researching java.util.regex.Matcher
API. Good luck.
Below method converts charater index in ALL lines into that in SINGLE line.
So you can use it in android programming like, getInLineOffsetFromStringOffset(editText.getText(),editText.getSelectionStart)
I have tested in all conditions like, single line or multiple line (some part of the code seems nesty due to controlling such behaviour), so you just copy and paste if you don't have time to investigate what these mean.
Note: To get your desired result as the question described, you just need to plus one the result.
public static int getInLineOffsetFromStringOffset(CharSequence src,int offset){
Pattern p=Pattern.compile("(\r)|(\n)|(\n\r)");
Matcher m=p.matcher(src);
int x=offset;
int start=0,end=0,lastEnd=0;
if(m.find()){
end=m.end();
start=m.start();
if(!(end>x))
while(m.find()){
lastEnd=end;
end=m.end();
start=m.start();
if(end>x)
break;
}
}
if(!(end>x)){
lastEnd=end;
start=src.length();
end=src.length();
}
return (start<x)?-1:(x-(lastEnd));
}