Rishi Rishi - 1 year ago 111
Android Question

IndexOutOfBoundsException in Edit Text for android

i have an edit text in my activity.i am entering numbers in it manually but

int mystart = destinationNumber.getSelectionStart();
int myend = destinationNumber.getSelectionEnd();
numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
"1", 0, 1);

its entering fine according to the cursor position.

i have a delete button in my acitivity which deletes single character according to cursor postion.

numberText.getText().delete(myend - 1, mystart);

But this logic is not working properly when i select whole text and call delete method it gives me IndexOutOfBoundsException OR i select 4-5 digits and call this delete.

I want the same functionality as android contact dialpad number enter field.Can someone help me figure out what is the correct logic to delete single digit from edittext and multiple selected digits as well.


Answer Source

delete receives the start as first parameter and end as second, not the other way around.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download