Skip to content

Spoke Right

Education for everyone

https://spokeright.com
Primary Menu
  • Home
  • Our Services
    • Translation
    • Interpretation
    • Transcription
    • Voice-Over
    • Proofreading
    • Content writing
  • Free Education
    • Learn Android Studio
    • Learn Python
    • Learn MongoDB
    • Learn MySql
    • Learn React
    • Montessori Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
    • TicTac
    • Checkers
    • Foosball
    • Billiards
    • Master Chess
    • Logic Game
  • Contact Us
Download Now
  • Home
  • Learn Android Studio
  • How do I use single editTexts with text watcher
  • Learn Android Studio

How do I use single editTexts with text watcher

Spoke Right November 11, 2022 2 min read

edit texts with text watcher

Android EditText with TextWatcher 

The TextView class has a subclass named Android EditText which is used for entering and modifying text. The input type must be defined in the inputType property of EditText while using EditText width. It thus configures the keyboard according to the input. To watch the changes made over EditText, the TextWatcher interface is used by the EditText, for which the addTextChangedListener() method is called by the EditText.

Methods of TextWatcher:

  • beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3): Before making any change over EditText, the beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) method is executed.
  • onTextChanged(CharSequence cs, int arg1, int arg2, int arg3): While making any change over EditText, the onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) method is executed.
  • afterTextChanged(Editable arg0): After change is made over EditText, the afterTextChanged(Editable arg0) method is executed.

Example of EditText with TextWatcher():

In the below example, we are demonstrating the implementation of the EditText with TextWatcher to search data from ListView.

activity_main.xml:

In the activity_main.xml file, we will add the EditText and ListView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">
 
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:inputType="text"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
 
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Activity class:(File: MainActivity.java)

package com.example.radioapp;
 
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    private ListView lv;
    private EditText editText;
    private ArrayAdapter<String> adapter;
 
    private String products[] = {"A1", "B2","C3", "D4", "E5", "F6",
            "G7", "H8","I9", "J10"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        lv = (ListView) findViewById(R.id.listView);
        editText = (EditText) findViewById(R.id.editText);
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
 
        editText.addTextChangedListener(new TextWatcher() {
 
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                adapter.getFilter().filter(cs);
            }
 
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
            }
 
            @Override
            public void afterTextChanged(Editable arg0) {
                Toast.makeText(getApplicationContext(),"Text changed",Toast.LENGTH_LONG).show();
            }
        });
    }
}

Continue Reading

Previous: How do i search veiw android
Next: How do I put the Search view on my Android toolbar

Related Stories

How to Get Imageview From Sqlite Database Android Studio
1 min read
  • Learn Android Studio

How to Get Imageview From Sqlite Database Android Studio

November 28, 2022
How do you show a toast on Android
1 min read
  • Learn Android Studio

How do you show a toast on Android

November 28, 2022
How do I completely Android Java Close App
1 min read
  • Learn Android Studio

How do I completely Android Java Close App

November 27, 2022

This AD Will support Us

Support Us

Coding Ustad LTD Support
Coding Ustad LTD Support

Coding Ustad LTD

Coding Ustad LTD
Coding Ustad LTD

Recent Posts

  • Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
  • How to Install Node.js and npm on CentOS 7
  • How to Install Node.js and npm on CentOS 7
  • How to Install CentOS Web Panel (CWP) on CentOS 7
  • The Power of Marketing in Real Estate: A Guide to Boost Your Business

Get free Hosting

Hostens.com - A home for your website
Ahsan Nadeem

You may have missed

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
2 min read
  • Spoke Right News

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes

July 18, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 3, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 2, 2023
How to Install CentOS Web Panel (CWP) on CentOS 7
3 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install CentOS Web Panel (CWP) on CentOS 7

May 2, 2023
  • Home
  • Our Services
  • Free Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
  • Contact Us
Copyright © All rights reserved. | MoreNews by AF themes.