0

i'm developing an application for android studio. I added buttons to move the cursor left and right, how can I also move it up and down between the lines? This is what I have already done:

EditText editText; ImageButton buttonBackward, buttonForward; RelativeLayout arrows;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = findViewById(R.id.editText);
    buttonBackward = findViewById(R.id.buttonBackward);
    buttonForward = findViewById(R.id.buttonForward);
    arrows = findViewById(R.id.arrows);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            arrows.setVisibility(View.VISIBLE);
        }
    });
    buttonForward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getSelectionEnd() < editText.getText().toString().length()) {
                editText.setSelection(editText.getSelectionEnd() + 1);
            } else {
                //fine stringa, non posso muovere il cursore
            }
        }
    });
    buttonBackward.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getSelectionStart() > 0) {
                editText.setSelection(editText.getSelectionEnd() - 1);
            } else {
                //start of string, cannot move cursor backward
            }
        }
    });
}
  • I hope this [answer](https://stackoverflow.com/a/62291446/9932178) will help you. – Abdul Rafay Jul 29 '21 at 17:32
  • Have you try android:nextFocusDown="@+id/.." android:nextFocusLeft="@+id/.." android:nextFocusRight="@+id/.." android:nextFocusUp="@+id/.." ? have a look on this link https://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android – Mujahid Khan Jul 29 '21 at 19:30
  • hello, thanks anyway but I have to move the cursor in the same edittext and not between two different edittext – maria giovanna Jul 31 '21 at 11:32

0 Answers0