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?
Asked
Active
Viewed 119 times
0
-
Maybe you need edittext.requestFocus();? – cherry-wave Oct 20 '16 at 11:13
-
but edittext is dynamic what ever edit text is clicked and reached to limit automatically go to the next edittext – PriyankaChauhan Oct 20 '16 at 11:14
-
Well then you still have to get the next edit text. When creating the edit texts dynamically. give them id's with increasing numbers so you can access them afterwards. – cherry-wave Oct 20 '16 at 11:15
-
1i think you have to use textwatcher in that you can check your edittext limit and make logic there – Vishal Thakkar Oct 20 '16 at 11:16
-
@cherry-wave means u saying need to store all ids in array when its clicked and reached to limit move to foucs to next edittext – PriyankaChauhan Oct 20 '16 at 11:17
-
@VishalThakkar I have 50 edittext for all need to create textwatcher , which is not proper way to do – PriyankaChauhan Oct 20 '16 at 11:19
-
You can add one TextWatcher for all. You get the editText in the event, can test if the limit is reached, and then get set focus to the next editText (by reading out the id of the current edit text and increasing the number) – cherry-wave Oct 20 '16 at 11:21
-
Do you have 1 textview with 50 rows or 50 textviews with 1 row ? – IIIIIIIIIIIIIIIIIIIIII Oct 20 '16 at 11:21
-
@MarkB 1 TabelRow with 1 EditView that is added 50 times – PriyankaChauhan Oct 20 '16 at 11:22
-
@cherry-wave okay Let me check that way, but how to add one TextWatcher for all – PriyankaChauhan Oct 20 '16 at 11:23
-
OK it seems TextWatcher doesn't get the actual EditText so you have to do a little twist: http://stackoverflow.com/questions/4283062/textwatcher-for-more-than-one-edittext – cherry-wave Oct 20 '16 at 11:30
-
@pcpriyanka if you dont want create new instance of text watcher then you can try like this.http://stackoverflow.com/questions/4283062/textwatcher-for-more-than-one-edittext – Vishal Thakkar Oct 20 '16 at 11:31
-
I would suggest @Vishal's first link, the other one we both posted isn't suitable for dynamic editTexts. I know, with this solution you have to create multiple TextWatchers, but this may be the only way. – cherry-wave Oct 20 '16 at 11:34
-
@cherry-wave vishal thanks let me try one – PriyankaChauhan Oct 20 '16 at 11:37
-
@VishalThakkar As you suggested I set Textwatcher for all EditText now how to setFocus for next editText – PriyankaChauhan Oct 20 '16 at 11:44
-
on which condition you want to set focus to next edittext – Vishal Thakkar Oct 20 '16 at 11:50
-
text limit for 25 – PriyankaChauhan Oct 20 '16 at 11:51
-
oh.then in just make condition like if charseq.length() >25 then focus to next edit text – Vishal Thakkar Oct 20 '16 at 11:53
-
@VishalThakkar thanks ! its working – PriyankaChauhan Oct 20 '16 at 11:59
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126228/discussion-between-pcpriyanka-and-vishal-thakkar). – PriyankaChauhan Oct 20 '16 at 12:01
1 Answers
1
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>

PriyankaChauhan
- 953
- 11
- 26