In this article, we are going to implement an important and quite interesting feature. While Chatting with your friends over various social media platforms you may have found that if your friend is typing something then it shows Typing. Here are going to implement the same. While we start typing something, It will show typing, and when we erase everything it will show Stopped Typing. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:orientation = "vertical" tools:context = ".MainActivity" > < EditText android:id = "@+id/check" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:hint = "Type Something" android:textSize = "22sp" android:textStyle = "bold" /> < TextView android:id = "@+id/confirm" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "10dp" android:hint = "Not typing" android:textSize = "22sp" android:textStyle = "bold" /> </ LinearLayout > |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { EditText msg; TextView confirm; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); msg = findViewById(R.id.check); confirm = findViewById(R.id.confirm); // When there is change in state of edittext input msg.addTextChangedListener( new TextWatcher() { @Override // when there is no text added public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (s.toString().trim().length() == 0 ) { // set text to Not typing confirm.setText( "Not Typing" ); } else { // set text to typing confirm.setText( " Typing" ); } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { confirm.setText( " Typing" ); } // after we input some text @Override public void afterTextChanged(Editable s) { if (s.toString().trim().length() == 0 ) { // set text to Stopped typing confirm.setText( "Stopped Typing" ); } } }); } } |