---
title: "TextWatchers, OnClickListeners in android"  
description: "TextWatchers, OnClickListeners in android"  
author: "Anonymous User"  
published: 2014-11-06  
updated: 2014-11-07  
canonical: https://www.mindstick.com/forum/2532/textwatchers-onclicklisteners-in-android  
category: "android"  
tags: ["textwatcher", "onclicklistener"]  
reading_time: 2 minutes  

---

# TextWatchers, OnClickListeners in android

Just wondering if there is any best [practice](https://yourviews.mindstick.com/story/1498/terrific-benefits-of-a-morning-meditation-practice) on the use of TextWatchers, OnClickListeners and such like with regards to how you [put](https://answers.mindstick.com/qa/111890/can-you-explain-the-difference-between-put-and-post-http-methods) them in [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii).

Would you do the following:

```
textBox.addTextChangedListener(new TextWatcher(){    public void afterTextChanged(Editable s)    {                        // some code here    }     public void beforeTextChanged(CharSequence s, int start, int count, int after)    {         //and/or here    }     public void onTextChanged(CharSequence s, int start, int before, int count)    {         //and/or even here    } });
```

Or would it be best to have the TextWatcher etc as a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) and call it like:

```
textBox.addTextChangedListener(myTextWatcher);
```

I understand the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) would be far better for [reusability](https://www.mindstick.com/articles/12157/object-oriented-programming) but was curious about if there is only one [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of the, in this example, TextWatcher.

## Replies

### Reply by Barbara Jones

I think there is the other option that is implementing the interface on your main class and then having something like this:

textBox.addTextChangedListener(myTextWatcher);

In total there are 3 options and I use 3 of them.

If the TextWatcher or the onClickListener need some configuration (eg. id or row number) and to be used multiple times, I create my own class and does something like this:

textBox1.addTextChangedListener(new MyTextWatcher(1, "Some Text"));

textBox2.addTextChangedListener(new MyTextWatcher(2, "Some Other Text"));

textBox3.addTextChangedListener(new MyTextWatcher(3, "Some Text 4"));

Other than that if it is a one time thing and is small, I go like this:

```
mButton.setOnClickListener(new OnClickListener() {    ....});
```

And I also use ButterKnife a lot. As far as I know it does not have any annotation for TextWatchers but it has onClickListener, onItemClickListener, onItemLongClickListener etc. And you can use one function for multiple Views using annotations.


---

Original Source: https://www.mindstick.com/forum/2532/textwatchers-onclicklisteners-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
