I am able to add Textwatcher listener for dynamically created Edittext so basically I'll check the first editext ontextchanged if there is any value create another edittext and so on.How can check which edittext is called in the listener.
I have checked couple of links how-to-use-single-textwatcher-for-multiple-edittexts but the Ids are fixed in this case.
Code:
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout) findViewById(R.id.container);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
final EditText textOut = (EditText) addView.findViewById(R.id.textout);
textOut.addTextChangedListener(watcher);
container.addView(addView);
}
TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// addView();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
/*
* int childcount = container.getChildCount(); for (int i=0; i <
* childcount; i++){ View v = container.getChildAt(i);
*
* if(i == (childcount -1)){ Toast.makeText(getApplicationContext(),
* "Last count"+childcount, Toast.LENGTH_SHORT).show(); addView(); }
* }
*/
}
};
public void addView() {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
final EditText textOut = (EditText) addView.findViewById(R.id.textout);
textOut.addTextChangedListener(watcher);
addView.setTag(count++);
container.addView(addView);
}
}