1

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.

zimmerrol
  • 4,436
  • 2
  • 17
  • 36

2 Answers2

0

After some more experimenting I have finally thought of something 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 was at 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 the text is too long for the control
            if (width >= cwidth) {
                layout = ((EditText) v).getLayout();
                x = event.getX() - scroll_down_x;
                scroll_down_x = event.getX();
                //do not scroll over the left border
                if (((EditText) v).getScrollX() + (int)x >= 0) {
                    //do not scroll over the 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 the user to set the cursor and to scroll through a one lined EditText and it will hide the system keyboard. I hope that it will help somebody. I'm still interested if there is not a more convenient way to archive this behaviour.

zimmerrol
  • 4,436
  • 2
  • 17
  • 36
-1

if you want to show any information without edit it, you could use a TextView (see this post https://stackoverflow.com/a/3256305/2697368 ) instead of Edittext, but if you want an EditText, try to use below code:

<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cursorVisible="false"
    android:focusable="false"
    android:inputType="textMultiLine"
    android:scrollbars="vertical"
    android:text="Long Text here" />

If you want to enable user edition, use this code:

EditText edittext = (EditText) findViewById(R.id.edittext);

edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }
});
}
Community
  • 1
  • 1
Gorio
  • 1,496
  • 2
  • 17
  • 32
  • 1
    This does not really solve the problem because the user is still able to select parts of the text - only the command-buttons to copy or share it are vanished. – zimmerrol Apr 06 '16 at 22:55
  • What version of Android have you tested my solution ? – Gorio Apr 07 '16 at 00:04
  • Okay, I forgot to say that the user still has to be able to change the cursor position - that's my bad. If you edit your post I can give you an upvote again. – zimmerrol Apr 07 '16 at 08:11
  • @FashTek , try my new solution. I've added a tag android:cursorVisible="false" into EditText – Gorio Apr 07 '16 at 11:25