Hi in my app there are 2 editText which must display value if the edittext input is from some method and if the input to editText is from keyboard then it must call textwatcher.
that is:
if the edittext1 has input from virtual keyboard then trigger TextWatcher which will call a method and display value in edittext2[here edittext2 acts as textview]
if the edittext2 has input from virtual keyboard then trigger textwatcher which will call a method and display result in edittext1[here edittext1 acts as textview]
is this possible? if so please tell me the concepts to follow.
thanks
Directly implementing would probably CRASH your app. You can do a work around by using flag variable
public class MainActivity extends Activity {
EditText et1, et2;
String flag = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
// Here update the flag variable to keep track which text view is in use
et1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flag = "ET1";
}
});
// Here update the flag variable to keep track which text view is in use
et2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flag = "ET2";
}
});
et1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Set the value of editText2 value here if only flag contains
// ET1
// This will prevent changes if ET2 is being used
if (flag.equals("ET1")) {
et2.setText(s);
}
}
});
et2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Set the value of editText1 value here only if flag equals ET2
if (flag.equals("ET2")) {
et1.setText(s);
}
}
});
}
}
Run Here double click on a EditText before you type on them. You can further improve this by enabling and disabling the edittext in appropriate situations and hence by doing so you can achieve the same by a single click.
If you have a question about the code, please comment below.