Get Cursor Position in Android in Edit Text

Get Cursor Position in Android in Edit Text?

You can get the Cursor position using the getSelectionStart() and getSelectionEnd() methods. If no text is highlighted, both getSelectionStart() and getSelectionEnd() return the position of the cursor. So something like this should do the trick:

myEditText.getSelectionStart();

or

myEditText.getSelectionEnd();

Then if you want to select all the text after the cursor you could use this:

int cursorPosition = myEditText.getSelectionStart();
CharSequence enteredText = myEditText.getText().toString();
CharSequence cursorToEnd = enteredText.subSequence(cursorPosition, enteredText.length());

How to get cursor position in a Edittext

Use getSelectionStart() method to get current position of cursor in an Edittext

Get word from EditText on current cursor position

EditText et = (EditText) findViewById(R.id.xx);

int startSelection = et.getSelectionStart();

String selectedWord = "";
int length = 0;

for(String currentWord : et.getText().toString().split(" ")) {
System.out.println(currentWord);
length = length + currentWord.length() + 1;
if(length > startSelection) {
selectedWord = currentWord;
break;
}
}

System.out.println("Selected word is: " + selectedWord);

How to set cursor position in EditText?

Where position is an int:

editText1.setSelection(position)

set cursor position of edittext in center...!

I found now what is main problem...
Problem is in the API level..

I change API level of my program.

Below API 15, cursor is set in the center in my application..

From API 16, cursor position set to start of the Hint text...!!!

Set cursor position in android Edit text

You can achieve this by using :

Edittext.setSelection(Edittext.length());

this will put the cursor position in the end. otherwise you can get the text in a string and check the text position where you want to put the cursor and do it like this

String text = edittext.getText().toString();

for(int i = 0; i <text.length();i++)
{
//your logic here to check the position of text
}

and then

Edittext.setSelection(position);

Get screen coordinates of cursor current line EditText

This can be rather easily accomplished by subclassing EditText, and inserting the highlight effect in its onDraw() method, before everything else is drawn. EditText's Layout object can give us the line number from the current cursor position, with which we can get the bounds as a Rect. We then draw the highlight rectangle, and call the super method to draw the rest of the View.

For example:

public class HighlightEditText extends EditText {

private static final int HIGHLIGHTER_YELLOW = 0x88f3f315;

private Rect lineBounds;
private Paint highlightPaint;
private int lineNumber;
private boolean lineHighlightEnabled = true;

public HighlightEditText(Context context) {
this(context, null);
}

public HighlightEditText(Context context, AttributeSet a) {
super(context, a);

lineBounds = new Rect();
highlightPaint = new Paint();
highlightPaint.setColor(HIGHLIGHTER_YELLOW);
}

@Override
protected void onDraw(Canvas canvas) {
if (lineHighlightEnabled) {
lineNumber = getLayout().getLineForOffset(getSelectionStart());
getLineBounds(lineNumber, lineBounds);

canvas.drawRect(lineBounds, highlightPaint);
}

super.onDraw(canvas);
}

public void setLineHighlightEnabled(boolean enabled) {
lineHighlightEnabled = enabled;
invalidate();
}

public boolean isLineHighlightEnabled() {
return lineHighlightEnabled;
}

public void setLineHighlightColor(int color) {
highlightPaint.setColor(color);
if (lineHighlightEnabled) {
invalidate();
}
}

public int getLineHighlightColor() {
return highlightPaint.getColor();
}
}

You can include this custom class in your layouts as usual. For example:

<com.mycompany.myapp.HighlightEditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
android:imeOptions="flagNoExtractUi" />

Please note that if you're using the AppCompat library and its amenities, you should instead extends AppCompatEditText, to ensure the tinting and whatnot are handled appropriately.



Related Topics



Leave a reply



Submit