[JAVA] How to get values in real time with TextWatcher (Android)

Overview

How to reflect (display) characters in TextView when writing characters with EditText. This can be achieved by using TextWatcher.

Implement TextWatcher (implements)

public class MainActivity extends AppCompatActivity implements TextWatcher{
...
}

Implement TextWatcher in the Activity or Fragment you want to display.

Override (beforeTextChanged / onTextChanged / afterTextChanged)

    @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) {
    }
}

1.beforeTextChanged(CharSequence s, int start, int count,int after) -Method called just before the string is modified ・ Char Sequence s → The character string currently entered in EditText ・ Int start → Start position of the newly added character string in the character string of s ・ Int count → Total number of changed strings in the s string

2.onTextChanged(CharSequence s, int start, int before, int count) ・ Called when one character is entered ・ Char Sequence s → The character string currently entered in EditText ・ Int start → Start position of the newly added character string in the character string of s ・ Int before → Number of existing strings to be deleted ・ Int count → Number of newly added character strings

3.afterTextChanged(Editable s) -Finally this method is called ・ Editable s → The final modifiable, modified string

You can set before, during, and after character input in real time.

Listener registration

addTextChangedListener(this);

Register the target EditText in the listener.

Sample code

MainActivity.java


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements TextWatcher {

    EditText editText;
    TextView textView;
    EditText EditText_OUT;

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

        textView = findViewById(R.id.textView);
        editText = findViewById(R.id.editText);

        EditText_OUT = (EditText) findViewById(R.id.editText);
        //Register listener
        EditText_OUT.addTextChangedListener(this);
    }

    @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) {
        //Reflect the value entered in TextView in real time
        textView.setText(s);
    }
}

activity.main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.308" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="284dp"
        android:layout_height="79dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

ezgif-1-3dd9d81e7cfb.gif

Recommended Posts

How to get values in real time with TextWatcher (Android)
How to get started with slim
How to get parameters in Spark
How to get along with Rails
How to monitor application information in real time using JConsole
How to get boolean value with jQuery in rails simple form
How to get date data in Ruby
[Android] How to deal with dark themes
[Note] How to get started with Rspec
How to get the date in java
How to use ExpandableListView in Android Studio
[Rails] How to get the user information currently logged in with devise
How to get keycloak credentials in interceptor class
How to get Class from Element in Java
How to get started with Eclipse Micro Profile
How to get resource files out with spring-boot
How to get the ID of a user authenticated with Firebase in Swift
[Rails] How to get rid of flash messages in a certain amount of time
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to sort in ascending / descending order with SQLite
How to call functions in bulk with Java reflection
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
How to switch Tomcat context.xml with WTP in Eclipse
How to get information about associated tables in many-to-many tables
How to use Z3 library in Scala with Eclipse
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
Where to get lost in specifications with Spark routing
How to delete untagged images in bulk with Docker
[Android] How to get the setting language of the terminal
How to use JDD library in Scala with Eclipse
How to query Array in jsonb with Rails + postgres
How to get started with creating a Rails app
How to enable submit button every time with jquery
How to get jdk etc from oracle with cli
Link Docker log to AWS CloudWatch and monitor in real time with VS Code
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to get the class name / method name running in Java
How to take a screenshot with the Android Studio emulator
How to change application.properties settings at boot time in Spring boot
How to number (number) with html.erb
How to get started with Gatsby (TypeScript) x Netlify x Docker
How to update with activerecord-import
How to make an app using Tensorflow with Android Studio
How to implement one-line display of TextView in Android development
Mapping to a class with a value object in How to MyBatis
How to fix Android apps crashing with RenderThread mystery error
Now is the time to get started with the Stream API
How to get started with JDBC using PostgresSQL on MacOS
How to set up a proxy with authentication in Feign
Android: How to deal with "Cause: cannot find android.support.transition.R $ id 2: android.support.transition.R $ id found in android / support / transition / R $ id 2.class"
How to deal with 405 Method Not Allowed error in Tomcat + JSP
How to make a jar file with no dependencies in Maven
Android: How to deal with "Could not determine java version from '10 .0.1'"
How to switch Java version with direnv in terminal on Mac
How to run a job with docker login in AWS batch
Source to display character array with numberPicker in Android studio (Java)
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to install Adopt OpenJDK on Debian, Ubuntu with apt (-get)