Reputation:
how make auto scroll in EditText?
the scrollTo(x,y) dont works... =/
final EditText edittext1 = (EditText) findViewById(R.id.editText1);
final EditText edittext2 = (EditText) findViewById(R.id.editText2);
//edittext1.setKeyListener(null);
edittext2.requestFocus();
edittext2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edittext1.setText(edittext1.getText() + "\n"
+ edittext2.getText() + "\n\n");
funcoes(edittext2.getText().toString());
edittext1.scrollTo(0, edittext1.getLayout().getLineCount());
edittext2.setText("");
return true;
}
return false;
}
});
I've tried colocoar within a ScrollView, but does not work very well. Has some way to do this scroll in EditText?
thanks
Upvotes: 0
Views: 2307
Reputation: 436
Try replacing this:
edittext1.scrollTo(0, edittext1.getLayout().getLineCount());
with this:
int y = (edittext1.getLineCount() - 1) * edittext1.getLineHeight(); // the " - 1" should send it to the TOP of the last line, instead of the bottom of the last line
edittext1.scrollTo(0, y);
because scrollTo(x,y)
measures x and y in pixels, and you were sending it the number of lines. getLineHeight
returns the number of pixels per line, so
lineHeight * lineCount = total pixel height.
(Also, I'm guessing you wanted the LineCount of edittext1, not of its parent layout.)
Upvotes: 1