android - EditText: allow scrolling but disable selecting -


i trying create edittext in no text can selected, can still scolled through user. archive first function created custom ontouchlistener , responded action_down events 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 @ end of text                         else                             mtext.setselection(offset - 1);                     break;          }          return true;      } }; mtext.setontouchlistener(otl); 

but disable scrolling. have tried reimplement 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 text long 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 not work @ - scrolling not smooth , inaccurate. so, how can improve "scrolling"-code or solve problem of edittext in no text can selected user can still scroll?

edit: user should still able change cursor position.

after more experimenting have thought of working.

final edittext mtext = (edittext) findviewbyid(r.id.input); view.ontouchlistener otl = new view.ontouchlistener() {    private float scroll_down_x;    final static int right_border = 20;     @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 @ end of text                 else                     mtext.setselection(offset - 1);             break;         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 text long control             if (width >= cwidth) {                 layout = ((edittext) v).getlayout();                 x = event.getx() - scroll_down_x;                 scroll_down_x = event.getx();                 //do not scroll on left border                 if (((edittext) v).getscrollx() + (int)x >= 0) {                     //do not scroll on right border                     if ((int)cwidth+((edittext) v).getscrollx()-right_border + x <= (int)width)                     {                         offset = layout.getoffsetforhorizontal(0, x);                         mtext.scrollby((int) x, 0);                     }                     else                     {                         mtext.scrollto((int)(width-cwidth+right_border), 0);                     }                 }                 else                 {                     mtext.scrollto(0, 0);                 }             }        }        return true;    } }; 

this code allows user set cursor , scroll through 1 lined edittext , hide system keyboard. hope somebody. i'm still interested if there not more convenient way archive behaviour.


Comments