Reputation: 953
I have created one TableLayout in which I am dynamically adding 50 Rows with EditText. I set the text limit for a single edit text such that if the 1st row reaches to limit, cursor of EditText moves to the next row dynamically. How can I do that? Any idea?
Upvotes: 0
Views: 123
Reputation: 953
Here I found solution
for (int i = 0; i < 50; i++) {
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText dueAmountText = new EditText(getApplicationContext());
dueAmountText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 5.0f));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(26f);
dueAmountText.setMaxLines(1);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(TextLength);
dueAmountText.setFilters(FilterArray);
Drawable drawable = dueAmountText.getBackground(); // get current EditText drawable
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
dueAmountText.setBackground(drawable); // set the new drawable to EditText
allwtEditTextList.add(dueAmountText);
newRow.addView(dueAmountText);
tableLayout.addView(newRow);
}
for (int z = 0; z < allwtEditTextList.size(); z++) {
final int pos = z;
allwtEditTextList.get(z).addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
if (s.length() >= TextLength) {
Log.e("TextCount", pos + "--" + allwtEditTextList.size());
if (pos < (allwtEditTextList.size() - 1)) {
EditText editText = allwtEditTextList.get(pos + 1);
editText.requestFocus();
}
}
}
@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
// Need getTag() here
allwtEditTextList.get(pos).getTag();
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</ScrollView>
Upvotes: 1