This should be simple but I have tried about a dozen things here in stackoverflow on the topic and they either do nothing or crash.
I have a edittext field that starts with the last entry, like “12” and I want the initial cursor to be to the right of the 2 and it always starts to the left of the 1. That would be fine if there was a del key but with only a back key on the keyboard, the user must manually re-position the cursor before typing over the old text. There are several fields and he most likely wants to leave most of them alone. What I want if he wants to just edit the second field to have him hit “next” “bs”, “bs”, “45”, “next”, “done”. this way he doesn’t have to position the cursor at all.
————UPDATE———————-
Here is the main file
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
public class MainActivity extends Activity {
static EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
edit.setSelection(edit.getText().toString().length());
}
}
and here is the xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="60dp"
android:text="12"
android:nextFocusDown="@+id/textView4"
android:inputType="number"
android:ellipsize="end"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/edit"
android:layout_alignBottom="@+id/edit"
android:layout_toRightOf="@+id/edit"
android:text=":"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:text="00"
android:inputType="number"
android:textAppearance="?android:attr/textAppearanceLarge" />
Use
setSelection()on each EditText inonCreate():The cursor will still respect the user’s touch gestures, but trackball and other movements methods will start with the cursor on the right.