How to Detect Whether User Started or Stopped Typing in Edittext Widget Using Keyboard in Android App

In this article, I wish to explain the topic of “How to Detect Whether User Started or Stopped Typing in Edittext Widget Using Keyboard in Android App?” Shall we start the article to learn about it? Let’s go.

Detect Whether User Started or Stopped Typing in Edittext Widget Using Keyboard in Android App

Detect Whether User Started or Stopped Typing in Edittext Widget Using Keyboard in Android App

java code

package com.programmerworld.keyboardusage;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textViewStatus;
    private EditText editTextKeyboardTesting;

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

        textViewStatus = findViewById(R.id.textViewStatus);
        editTextKeyboardTesting = findViewById(R.id.editTextTextMultiLine);

        editTextKeyboardTesting.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                textViewStatus.setText("User is typing ...");
            }
        });

        editTextKeyboardTesting.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    textViewStatus.setText("User STOPPED");
                }
            }
        });
    }
}

xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textViewStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/keyboard_is_not_used"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.331" />

    <EditText
        android:id="@+id/editTextTextMultiLine"
        android:layout_width="247dp"
        android:layout_height="124dp"
        android:layout_marginStart="90dp"
        android:layout_marginTop="45dp"
        android:ems="10"
        android:gravity="start|top"
        android:hint="@string/type_here"
        android:inputType="textMultiLine"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="167dp"
        android:layout_marginTop="41dp"
        android:text="@string/done"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewStatus" />

</androidx.constraintlayout.widget.ConstraintLayout>
<resources>
    <string name="app_name">Keyboard usage</string>
    <string name="keyboard_is_not_used">Keyboard is not Used ...</string>
    <string name="type_here">Type here ...</string>
    <string name="done">Done</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.programmerworld.keyboardusage">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Final Words

I hope this article “How to Detect Whether User Started or Stopped Typing in Edittext Widget Using Keyboard in Android App” will be useful to you. If you face any issues in this execution? ask us via the comment section. Do share this article with other Android Developers to encourage us.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment