Makrand Gupta Makrand Gupta - 3 months ago 24
Android Question

Android imeOptions="actionDone" not working

I am trying to get a login screen for an Android app and so far this is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">

<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionNext">

<requestFocus />
</EditText>

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:singleLine="true"
android:imeOptions="actionDone" />

<Button
android:id="@+id/buttonLaunchTriage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/login" />
</LinearLayout>



</RelativeLayout>


When I try to run it, the keyboard shows the right keys but when I try to press done after entering the password, nothing happens. I am using this to handle the button press:

private void setupLoginButton() {
Button launchButton = (Button) findViewById(R.id.buttonLaunchTriage);
launchButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.patient_start_userName_value);
EditText password = (EditText) findViewById(R.id.patient_start_password_value);

try {
if(TriageApplicationMain.validateUser(username.getText().toString(),password.getText().toString(),getApplicationContext()))
{
Toast.makeText(StartActivity.this,
"Launching Triage Application", Toast.LENGTH_SHORT)
.show();
startActivity(new Intent(StartActivity.this, MainActivity.class));
}

else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
StartActivity.this);


// set dialog message
alertDialogBuilder
.setMessage("Incorrect Credentials")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});


// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}
}


catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

}


I know this is a lot of code, but if anyone could help me here it would be great. This is for a school project.

PS: I have searched through Google for a solid hour before posting this so please don't criticize for not doing that. If you find a link that is useful then please share.

Answer

You should set OnEditorActionListener for the EditText to implement the action you want to perform when user clicks the "Done" in keyboard.

Thus, you need to write some code like:

password.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // Do whatever you want here
            return true;
        }
        return false;
    }
});

See the tutorial at android developer site