Prasad Ainapure
Prasad Ainapure

Reputation: 19

On Edit Text to another on the pressing Enter?

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;
    }
});}

Upvotes: 1

Views: 2876

Answers (2)

MattMatt
MattMatt

Reputation: 905

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!

Upvotes: 1

Shobhit Puri
Shobhit Puri

Reputation: 26007

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 .

Upvotes: 2

Related Questions