I am trying to create a EditText
in which no text can be selected, but which can still be scolled through by the user.
To archive the first function a created a custom OnTouchListener
and only responded to the ACTION_DOWN
events like this:
View.OnTouchListener otl = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
scroll_down_x = event.getX();
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if(offset>0)
if(x>layout.getLineMax(0))
mText.setSelection(offset); // touch was at end of text
else
mText.setSelection(offset - 1);
break;
}
return true;
}
};
mText.setOnTouchListener(otl);
But this does also disable the scrolling. I have then tried to reimplement
the ACTION_MOVE
events with
case MotionEvent.ACTION_MOVE:
String input = ((EditText)v).getText().toString();
float width = ((EditText) v).getPaint().measureText(input);
float cwidth = ((EditText)v).getWidth();
//only scroll when the text is too long for the control
if (width >cwidth )
{
layout = ((EditText) v).getLayout();
x = event.getX() - scroll_down_x;
offset = layout.getOffsetForHorizontal(0, x);
mText.scrollBy((int) x, mText.getScrollY());
}
return true;
But this does not work at all - the scrolling is not very smooth and also very inaccurate.
So, how can I improve this "scrolling"-code or solve the problem of an EditText
in which no text can be selected but the user can still scroll?
EDIT: Also the user should still be able to change the cursor position.