Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:20:08+00:00 2026-05-24T07:20:08+00:00

I am trying to make a FastSelectEditText, so that: Text can be selected by

  • 0

I am trying to make a FastSelectEditText, so that:

  1. Text can be selected by long click and slide the finger.
  2. When sliding and selecting, show a magnify glass(like iphone), so that user can see the text under her finger.

Unfortunately there is a problem with my design: The MagGlass shows only inside my FastSelectEditText. When user is selecting text in top lines, she can’t see the mag glass.
So I have to use this work around: show the mag glass lower than the finger, when it reaches the top of the FastSelectEditText.

I understand if I use another view for Mag Glass, that won’t be a problem. But to keep code simple, I think it’s better to keep the Mag Glass inside the FastSelectEditText.

Is there a way to draw something outside the bound of a view?
Or should I write another view(instead of some code inside the customized EditText) to implement a Mag Glass?(And probably put these views inside a frame layout?)

The mag glass is showing upper than the finger touched
The mag glass is showing lower than the finger touched

public class FastSelectEditText extends EditText implements OnLongClickListener {

/**
 * @param context
 */
public FastSelectEditText(Context context) {
    super(context);
    init();
}

/**
 * @param context
 * @param attrs
 */
public FastSelectEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public FastSelectEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private MagGlass mMagGlass;
private float mScale;
private void init(){
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    mScale = metrics.density;
    setGravity(Gravity.TOP);
    setOnLongClickListener(this);
    mMagGlass = new MagGlass();
}

private int getOffset(int x, int y){
    Layout layout = getLayout();
    int row = layout.getLineForVertical(getScrollY()+y-getPaddingTop());
    return layout.getOffsetForHorizontal(row, x-getPaddingLeft());
}

/**
 * the position/index when touch down.
 */
private int mDownOffset = 0;
private int mOldSelStart, mOldSelEnd;
/**
 * Did the user moved his finger after down event?
 */
private boolean mMoved = false;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    mMagGlass.setObjectCenter(x, y);
    boolean result;
    switch (event.getAction()){
    case MotionEvent.ACTION_DOWN:
        mOldSelStart = getSelectionStart();
        mOldSelEnd = getSelectionEnd();
        if (mOldSelStart != mOldSelEnd){
            startSlideAndSelect();
        }
        mDownOffset = getOffset(x, y);
        return super.dispatchTouchEvent(event);
    case MotionEvent.ACTION_MOVE:
        result = super.dispatchTouchEvent(event);
        int offset = getOffset(x, y);
        if (!mMoved && mDownOffset != offset){
            mMoved = true;
        }
        if (mSlideAndSelect){            
            if (mMoved){
                setSelection(mDownOffset, offset);
            }
            return true;
        }
        return result;
    case MotionEvent.ACTION_UP:
        boolean moved = mMoved;
        // reset mMoved
        mMoved = false;

        boolean longClicked = mLongClicked;
        mLongClicked = false;

        if (mSlideAndSelect && moved){
            event.setAction(MotionEvent.ACTION_CANCEL);
        }
        result = super.dispatchTouchEvent(event);
        if (mSlideAndSelect){
            mSlideAndSelect = false;
            int upOffset = getOffset(x, y);
            if (!moved && mDownOffset == upOffset && longClicked){
                setSelection(mOldSelStart, mOldSelEnd);
                showContextMenu();
            }else{
                setSelection(mDownOffset, upOffset);
            }
            return true;
        }
        return result;
    case MotionEvent.ACTION_CANCEL:
        mSlideAndSelect = false;
        // reset mMoved
        mMoved = false;
        mLongClicked = false;
        return super.dispatchTouchEvent(event);
    default:
        return super.dispatchTouchEvent(event);
    }
}

protected void startSlideAndSelect() {
    mSlideAndSelect = true;
    ViewParent parent = getParent();
    if (parent != null){
        parent.requestDisallowInterceptTouchEvent(true);
    }
}

private boolean mSlideAndSelect = false;
private boolean mLongClicked = false;

private Vibrator mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
@Override
public boolean onLongClick(View v) {
    if (!mMoved){
        startSlideAndSelect();
        mLongClicked = true;
        mVibrator.vibrate(30);
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mSlideAndSelect){
        mMagGlass.draw(canvas);
    }
}

/**
 * Need a drawable.mag_glass to work.
 * 
 * @author lifurong
 *
 */
class MagGlass{
    private int mWidth, mHeight;
    private Bitmap mMagGlassBitmap;
    private int mX, mY;
    private final static int INSET = 10;

    public MagGlass(){
        mMagGlassBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mag_glass);
        mWidth = mMagGlassBitmap.getWidth();
        mHeight = mMagGlassBitmap.getHeight();
    }
    public void setObjectCenter(int x, int y){
        mX = x;
        mY = y;
    }

    public void draw(Canvas canvas) {
        final float left = mX-mWidth/2.0f;
        final float top = mY-mHeight/2.0f;
        final float right = mX+mWidth/2.0f;
        final float bottom = mY+mHeight/2.0f;
        float vTrans = 80*mScale;
        int vTransSign;
        int[] location = new int[2];
        getLocationInWindow(location);
        int topEdge = location[1]-getPaddingTop()>0? 0:-location[1]+getPaddingTop();
        if (top-vTrans > topEdge){
            vTransSign = -1;
        }else{
            vTransSign = 1;
        }
        canvas.translate(0, vTrans*vTransSign);
        canvas.clipRect(left, top, right, bottom);
        canvas.drawBitmap(mMagGlassBitmap, left, top, null);
        canvas.clipRect(left+INSET, top+INSET, right-INSET, bottom-INSET);
        FastSelectEditText.super.onDraw(canvas);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:20:09+00:00Added an answer on May 24, 2026 at 7:20 am

    To draw outside the bound of a view, you need to set the view’s parent’s clipChildren to false.

    By default a ViewGroup has the clipChildrenset to true, which caused the children to draw on a canvas clipped to their bounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a sliding panel where you can click next and back to
Trying to make a make generic select control that I can dynamically add elements
Trying to make a simple toggle menu, and I can't seem to hide/show the
I'm trying make a static text which if too long for the sizer, will
Trying to make a regex that can handle input like either: Beverly Hills, CA
I am trying make long screen to vertical direction. So, I need a screen
Trying to make a custom :confirm message for a rails form that returns data
Im trying to make a function that will return an element of type point:
'm trying to make a small modification to django lfs project, that will allow
Im trying to make a universal parser using generic type parameters, but i can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.