Tuesday, October 4, 2016

android - EditText IME action done

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dbdef9"
    >
    <!--
        android:imeOptions

            Additional features you can enable in an IME associated with an editor to
            improve the integration with your application. The constants here correspond
            to those defined by imeOptions.

            Must be one or more values (separated by '|').

            actionDone
                The action key performs a "done" operation, closing the soft
                input method. Corresponds to IME_ACTION_DONE.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:hint="Write some text here."
        android:imeOptions="actionDone"
        android:inputType="text"
        android:maxLines="1"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private EditText mEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mEditText = (EditText) findViewById(R.id.et);

        /*
            setOnEditorActionListener

                void setOnEditorActionListener (TextView.OnEditorActionListener l)

                Set a special listener to be called when an action is performed on the text view.
                This will be called when the enter key is pressed, or when an action supplied to
                the IME is selected by the user. Setting this means that the normal hard key
                event will not insert a newline into the text view, even if it is
                multi-line; holding down the ALT modifier will, however, allow the user
                to insert a newline character.
        */

        // Set an editor action listener for edit text
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                // If user press done key
                if(i == EditorInfo.IME_ACTION_DONE){
                    // Get the input method manager
                    InputMethodManager inputMethodManager = (InputMethodManager)
                            mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    // Hide the soft keyboard
                    inputMethodManager.hideSoftInputFromWindow(mEditText.getWindowToken(),0);

                    // Show the user inputted text in the snack bar
                    Snackbar.make(
                            mCLayout,
                            "Inputted : " + mEditText.getText().toString(),
                            Snackbar.LENGTH_LONG
                    ).show();

                    return true;
                }
                return false;
            }
        });
    }
}