androidtextwatcherandroid-textwatcher

Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged


In my Android project, I have had to add a TextChangedListener (TextWatcher) to an edit text view. And there are three parts to it:

What are the differences of these three? I have had to implement a search of a table on the key listener and for my case all these three looked the same. Also they functioned the same. When I input a part of a product name, the table redraws with only those products that contain entered text in it. But I used the afterTextChanged() part. My code is:

EditProduct.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

            // System.out.println("onTextChanged"+s);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            // System.out.println("beforeTextChanged"+s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            // System.out.println("afterTextChanged"+s);

            String new_prx = s.toString();

            System.out.println(s);
            mini_productList = new ArrayList<Product>();

            // mini_productList
            int count = 0;
            if (new_prx.equals("")) {

                loadtableProducts(productList);

            } else {

                for (int i = 0; i < productList.size(); i++) {

                    if (productList.get(i).getDescription().toString()
                            .substring(0, (new_prx.length()))
                            .equalsIgnoreCase(new_prx)) {
                        mini_productList.add(productList.get(i));
                        count++;

                    }
                }

                loadtableProducts(mini_productList);
            }
        }
    });

So can someone give me an explanation on these three?


Solution

  • onTextChanged runs during the text changing.

    afterTextChanged runs immediately after the text is changed.

    beforeTextChanged runs the instant before the text is changed.

    Depending on when you want to assign variables or do things, you may want to run the code the instant before the change, or the instant after.

    Here is an example of this:

    String afterTextChanged = "";
    String beforeTextChanged = "";
    String onTextChanged = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        et = (EditText)findViewById(R.id.editText);
    
        et.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int st, int b, int c) 
            {
                onTextChanged = et.getText().toString();
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int st, int c, int a) 
            {
                beforeTextChanged = et.getText().toString();
            }
    
            @Override
            public void afterTextChanged(Editable s) 
            {
                afterTextChanged = et.getText().toString();
                Toast.makeText(Activity.this, "before: " + beforeTextChanged
                                               + '\n' + "on: " + onTextChanged 
                                               + '\n' + "after: " + afterTextChanged
                               ,Toast.LENGTH_SHORT).show();
            }
        });
    }
    

    In this case, let's say you changed the text from "h" to "hi", the output would be:

    before: "h"
    on: "hi"
    after: "hi"