1

i jave two editText boxes in my app, after having entered value in the first one i would like to move to next editText box on pressing Enter? This is what i have tried so far...

editText1.setOnKeyListener(new OnKeyListener(){

    public boolean onKey(View v, int keyCode, KeyEvent event){
        if((event.getAction()==KeyEvent.ACTION_DOWN)&&(keyCode==KeyEvent.KEYCODE_ENTER))
        {
            editText1.clearFocus();
            editText2.requestFocus();
            return true;
        }
        return false;
    }
});}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124

2 Answers2

2

Do you have android:singleLine="true" line added to you EditText in the layout XML? Or alternately you can set it by using setSingleLine() in code. This forces the edit text to use only one line and focus will go to the next EditText box when you'll press Enter. See this answer by @Scott on Handle enter key in EditText across different devices .

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • @PrasadAinapure Glad it helped you. Please accept one of the answers or give your own answer and accept it. This will mark the question solved. :) – Shobhit Puri Sep 02 '13 at 08:31
1

You can use many IME options in EditText XML Layout, and request focus on it. Example:

 <EditText
            android:id="@+id/EditText_Search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/custom_search_box"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:hint="Enter information"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:padding="10dp"
            android:saveEnabled="true"
            android:scrollHorizontally="true"
            android:textColor="@color/black" />
    <requestFocus />

The key to this is android:imeOptions set to "next". You could set the final editText in your View to android:imeOptions="done" and do something with that, maybe a Login or the like. And finally the end-tag <requestFocus />

I hope this helps, happy Coding!

MattMatt
  • 905
  • 6
  • 19