Voora Tarun
Voora Tarun

Reputation: 1216

android edittext request focus at specific position

I have an edittext view and button , Edit text has some default value. when i click on button i give focus to the edit text. but problem is cursor is at the start of edit text. how to place cursor after the default text.

what i did.

 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>

Upvotes: 4

Views: 1794

Answers (1)

Nikunj
Nikunj

Reputation: 4157

Google Docs here : EditText - setSelection, Selection - setSelection

add this line

mobile_text.setSelection(mobile_text.getText().length());

Upvotes: 6

Related Questions