IAmTheSquidward
IAmTheSquidward

Reputation: 582

Android - have IME enter key submit?

Is there a way to have the Enter key submit my form? Right now I have a Log In button that is sent using an addListenerButton()... Can I somehow link the Enter key to that as well? I think the code from my .java is irrelevant, but I can always post it if requested.

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:id="@+id/back"
android:background="@drawable/a_hdpi">

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:padding="10dp"
    android:background="@drawable/linearlayout_bg" >

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_top_bg"
    android:padding="10dp"
    android:hint="Username"
    android:textColorHint="#cccccc"
    android:drawableLeft="@drawable/user"  />

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_bottom_bg"
    android:layout_marginTop="-2dp"
    android:padding="10dp"
    android:hint="Password"
    android:singleLine="true"
    android:textColorHint="#cccccc"
    android:drawableLeft="@drawable/password" 
    android:password="true"
    android:imeOptions="actionSend" />

<Button
    android:id="@+id/submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_margin="4dp"
    android:text="Submit"
    style="@style/DefaultButtonText"
    android:background="@drawable/button_default_bg" />

Upvotes: 1

Views: 785

Answers (2)

andydev
andydev

Reputation: 1012

This can be done using the following apis: android:imeActionId with the following attributes android:imeOptions. Use this with the setOnEditorActionListener() method

Also this example might help you: http://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

Upvotes: 4

mattgmg1990
mattgmg1990

Reputation: 5876

Yes, find a reference to your EditText for which you have added android:imeOptions="actionSend" and set the onEditorActionListener to trigger the method or button onClick for login that you want to trigger.

EditText yourEditText = findViewById(R.id.password);
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            login(); // YOUR LOGIN METHOD HERE
            return true;
        }
        return false;
    }
});

Upvotes: 2

Related Questions