First of all, I've read the other questions on the matter, but I'm trying something different.
I have a multiline EditText with the XML below:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/textBodyColor"
android:id="@+id/editScriptET"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:textSize="@dimen/font20"
android:hint="@string/details"
android:fontFamily="@font/courier_prime"
android:inputType="textMultiLine"
/>
What I want to do is to set the cursor in the center of a line or at the left of a line (in that multiline EditText), when clicking on a menu item. So, when I click on a menu item I want the cursor to move at the center of the current line in the EditText. When pressing on another menu item, I need the cursor to go to the left of the current line. (All the menu items are always shown on the toolbar.)
Is there a way to do that?
I tried with scriptET.setGravity(Gravity.CENTER);
(where scriptET
is the refference to my EditText), but nothing happens.
EDIT: Using the advice recieved in the comments, I managed to set the cursor to the left with Selection.moveToLeftEdge(scriptET.getText(), scriptET.getLayout());
. Using some Toasts messages I've noticed that getLayout()
method gives null, yet the cursor is still moving to the left of the current line when clicking.
Also, moving the cursor in the 'logical' center is not possible when the line is empty, and I would really need that to be possible.
Any suggestion is highly appreciated!