Android edittext request request at specific position

I have an edittext view and a button, the edit text has some default value. when I click on the button, I pay attention to the edited text. but the problem is that the cursor is at the beginning of text editing. how to place the cursor after the default text.

What I've done.

 mobile_text =(EditText)findViewById(R.id.mobile_text);
 mobile_edit = (ImageView)findViewById(R.id.mobile_edit_icon);
        mobile_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mobile_text.setEnabled(true);
                mobile_text.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
        });

My layout.xml file

<RelativeLayout
        android:paddingTop="@dimen/pad_3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/pad_5dp"
        android:paddingBottom="@dimen/pad_5dp"
        >
        <TextView
            android:id="@+id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MOBILE"
            android:paddingLeft="2dp"
            android:textSize="@dimen/txt_12sp"

            />
        <EditText
            android:paddingTop="@dimen/pad_5dp"
            android:layout_below="@+id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:id="@+id/mobile_text"
            android:text="9581129423"

            />
        <ImageView
            android:clickable="true"
            android:id="@+id/mobile_edit_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/edit"
            android:layout_below="@+id/mobile"
            android:layout_alignParentRight="true"
            android:gravity="right|end"
            />

    </RelativeLayout>
+4
source share
1 answer

Google Docs: EditText - setSelection , Selection - setSelection

add this line

mobile_text.setSelection(mobile_text.getText().length());
+6
source

Source: https://habr.com/ru/post/1615018/


All Articles