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 7404861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:18:59+00:00 2026-05-29T05:18:59+00:00

I am using an EditText widget and would like to modify the context menu

  • 0

I am using an EditText widget and would like to modify the context menu that is displayed when the user long presses the view. The problem that I am having is that I need to know the character position within the text of the long press so I can determine what I need to add to the context menu. The base class is doing this because one of the choices in the menu is ‘Add “word_clicked_on” To Dictionary.’ Setting ClickableSpans within the text does not appear to be a solution since it consumes the click event which makes it impossible to move the edit cursor within the spans.

  • 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-29T05:19:00+00:00Added an answer on May 29, 2026 at 5:19 am

    Here is the solution that I came up with and it does work so I wanted to share it:

    First I concluded that I needed to extend the EditText class so that I could intercept the onTouchEvent, capture the ACTION_DOWN event, and save the position. Now that I have the position of the down point I can call getOffsetForPosition(downPointX, downPointY) and get the character position of the long-press. There is one big problem, getOffsetForPosition was not added until SDK 14! To make this solution work I had to back port the functionality of getOffsetForPosition and branch if the current SDK is earlier than SDK_INT 14. Here is the source code for the new class:

    public class ScrapEditText extends EditText{
    
    protected float LastDownPositionX, LastDownPositionY;
    
    public ScrapEditText(Context context) 
    {
        super(context);
    }
    
    public ScrapEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        final int action = event.getActionMasked();
        if(action == MotionEvent.ACTION_DOWN)
        {
            LastDownPositionX = event.getX();
            LastDownPositionY = event.getY();
        }
        return super.onTouchEvent(event);
    }
    
    public float GetLastDownPositionX()
    {
        return LastDownPositionX;
    }
    
    public float GetLastDownPositionY()
    {
        return LastDownPositionY;
    }
    
    public int GetOffsetForLastDownPosition()
    {
    
        if(Build.VERSION.SDK_INT > 13)
        {
            // as of SDK 14 the getOffsetForPosition was added to TextView
            return getOffsetForPosition(LastDownPositionX, LastDownPositionY);
        }
        else
        {
            return GetOffsetForPositionOlderSdk();
        }
    }
    
    public int GetOffsetForPositionOlderSdk()
    {
        if (getLayout() == null) return -1;
        final int line = GetLineAtCoordinateOlderSDK(LastDownPositionY);
        final int offset = GetOffsetAtCoordinateOlderSDK(line, LastDownPositionX);
        return offset;
    }
    
    public int GetLineAtCoordinateOlderSDK(float y)
    {
        y -= getTotalPaddingTop();
        // Clamp the position to inside of the view.
        y = Math.max(0.0f, y);
        y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y);
        y += getScrollY();
        return getLayout().getLineForVertical((int) y);     
    }
    
    protected int GetOffsetAtCoordinateOlderSDK(int line, float x) {
        x = ConvertToLocalHorizontalCoordinateOlderSDK(x);
        return getLayout().getOffsetForHorizontal(line, x);
    }
    
    protected float ConvertToLocalHorizontalCoordinateOlderSDK(float x) {
        x -= getTotalPaddingLeft();
        // Clamp the position to inside of the view.
        x = Math.max(0.0f, x);
        x = Math.min(getWidth() - getTotalPaddingRight() - 1, x);
        x += getScrollX();
        return x;
    }
    
    }
    

    In your Activity derived class:

    ScrapText = (ScrapEditText) findViewById(R.id.sample_text);  
    ScrapText.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    {
        int charOffset = FileText.GetOffsetForLastDownPosition();
    }           
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C#, I need a class called User that has a username, password, active
package com.android.SMStest; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button;
Question How do you save view widget instance state when, by using XML-defined widget
How to make an EditText look like a TextView that is done in Java
I know that when using a EditText we can change the input type using
If I disable an EditText widget using editText.setEnabled(false); I can still type into it
I am using a simple EditText and register an View.OnKeyListener . Some GUI changes
i am using android:imeOptions=actionSearch in editText and my question is can i write my
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have

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.