2

I know these type of question asked many time .but still nobody gave perfect answer for that.

I have question :

I want to move from EditText1 ** to another **EditText2 . I had already detect to editText1 but how to move cursor to editText2.?

In short I had to move my cursor position from one editText1 to another EditText2 directly.

full Error
  • 308
  • 3
  • 8
  • 21
  • When you want to move your cursor from Edittext1 to Edittext2 – Krishnakant Dalal May 01 '12 at 13:57
  • @KrishnakantDalal when I detect some special character into my first editText1 it will move to next editText2. I had been success to detect the special character but didn't know How to move it in realtime. – full Error May 02 '12 at 04:58

5 Answers5

8

I faced this type of issue and found the solution as below.

Here I have two editText, if I press "a", my cursor will move to next step. I used below code for doing it.

final EditText editText = (EditText) findViewById(R.id.editText1);

editText.setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(View v , int keyCode , KeyEvent event) {

          EditText editText2 = (EditText) findViewById(R.id.editText2);
          
        // TODO Auto-generated method stub
        if (keyCode == event.KEYCODE_A) {
             
            Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
            editText2.requestFocus();
        }

        return true;
    }
});

Let me know if you are facing any error regarding this.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
NovusMobile
  • 1,813
  • 2
  • 21
  • 48
  • Thanks for ans. sometimes it didn't work when I checked in on real device. – full Error May 02 '12 at 11:43
  • I found the better solution here http://stackoverflow.com/questions/12418324/android-move-cursor-from-one-edittext-to-another-one-if-click-any-letter-in-fiel – LOG_TAG Jul 15 '14 at 05:15
2

For this, all you need to do is...add below two properties to your EditText tag in xml, except the last EditText(In case, you add it to the last EditText also, then the cursor control will again go to the first EditText when you press enter/next from the keypad)

<EditText . . android:singleLine="true" android:imeOptions="actionNext" . . />

Hope this helps

Avi
  • 551
  • 5
  • 8
1

Here is a working example, hope this helps.

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText 
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some string of text"
        />

    <EditText 
        android:id="@android:id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some string of text"
        />

    <Button 
        android:id="@android:id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        />

</LinearLayout>

Class:

public class Example extends Activity {
    TextView text1;
    TextView text2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1 = (EditText) findViewById(android.R.id.text1);
        text2 = (EditText) findViewById(android.R.id.text2);
        Button button = (Button) findViewById(android.R.id.button1);

        button.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Selection.setSelection((Editable) text2.getText(), text1.getSelectionStart());
                text2.requestFocus();
            }
        });
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks for your answer. I want it into real time scenario . when I detect some special character into my first editText1 cursor will be moved to next editText2. I had been success to detect the special character but didn't know How to move it in Realtime. – full Error May 02 '12 at 04:59
1

Set onKeyListener to detect the key pressed on every key pressed checked your condition and when your condition will be fulfilled set edittext property edittext2.requestFocus();

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
0

I have tested all the previous code segments, and find all are working fine. But I find just calling "requestFocus()" with the proper edittext object is also working. As per as ques asked, the ans can be:

edittext2.requestFocus();

which is working fine for me. Please correct me if I am wrong.

noNameYet
  • 635
  • 2
  • 10
  • 15