I have 2 editText objecst in my android application. I want write to such a logic so that if I type something in textField 1, then same should be replicated in Textfield 2 in runtime and vice-versa.
Example: if i type “a” in field1, field2 will also have “a”. If I type “b” in field2, field should also change to “b”
I am able to achieve the same one way i,e from 1 to 2 or 2 to 1 but not simultaneously. Below is the code I wrote:
final EditText tf1 = (EditText)findViewById(R.id.editText1);
final EditText tf2 = (EditText)findViewById(R.id.editText2);
tf1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
tf1.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s) {tf2.setText(s);}
}
}
}
};
Similar way I wrote another listener for tf2 and set the text in tf1. Kindly help on how can this scenario can be handled
Your answer is almost there. The key here is to remove the textChangedListener for either of the EditText fields if it loses focus. Then, keep track of which EditText field currently has the listener, and update the other accordingly. Here’s a simple activity that should accomplish what you’re looking for: