Nilesh
Nilesh

Reputation: 532

Android: How to move the cursor down to next line in the multiline Edittext?

I have a requirement where i have to implement Buttons which can move the cursor up and down the line in a multi line Edittext.

I went through the Developer website and found that Selection class http://developer.android.com/reference/android/text/Selection.html can be used, but i am not able to use it..

Please help me here..

Thank you.

Upvotes: 2

Views: 2901

Answers (3)

bansalakshay8
bansalakshay8

Reputation: 43

Try:

editText.append("\n");

It will move cursor to next line.

Upvotes: 0

Fajar Rukmo
Fajar Rukmo

Reputation: 78

Source: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/text/Selection.java

I copied the code and made a little modification below, then appended it to my class.


public boolean moveDown(Layout layout) {
        int start = myTextbox.getSelectionStart();
        int end = myTextbox.getSelectionEnd();

        if (start != end) {
            int min = Math.min(start, end);
            int max = Math.max(start, end);

            myTextbox.setSelection(max);

            if (min == 0 && max == myTextbox.length()) {
                return false;
            }

            return true;
        } else {
            int line = layout.getLineForOffset(end);

            if (line < layout.getLineCount() - 1) {
                int move;

                if (layout.getParagraphDirection(line) ==
                    layout.getParagraphDirection(line + 1)) {
                    float h = layout.getPrimaryHorizontal(end);
                    move = layout.getOffsetForHorizontal(line + 1, h);
                } else {
                    move = layout.getLineStart(line + 1);
                }

                myTextbox.setSelection(move);
                return true;
            }
        }

        return false;
    }

I hope this will be useful.

If somebody knows how to use real method without recreating it, please share :D

Upvotes: 1

Brijesh Patel
Brijesh Patel

Reputation: 2958

I am not sure about this code but it may help you to some extend....

just go through the code from the following url. i might help you to find out solution.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/text/Selection.java

Upvotes: 1

Related Questions