Android- How can I show text selection on textview?
Asked Answered
G

2

12

I am implementing a epub reading app where I am using textview for showing text of epub. I want to select text from textview when user long presses on textview and then do multiple operations on selected text of textview like highlight etc.. So, How can I show those cursors to user to select text whatever user wants.

*I dont want to use EditText and make it look like textview. May be overriding textview is prefered.

*I have attached screenshot to explain what I am looking for-

Screenshot from moon+reader to show text selection in it which I want in my app

Gastroscope answered 10/9, 2012 at 6:49 Comment(2)
sorry the image does not loaded successfully...try it againAv
I can see the image at my side :\.. i ll reupload itGastroscope
P
18

This is asked long time ago, when I had this problem myself as well. I made a Selectable TextView myself for my own app Jade Reader. I've hosted the solution to GitHub. (The code at BitBucket ties to the application, but it's more complete and polished.)

Selectable TextView (on GitHub)

Jade Reader (on BitBucket)

selection

Using the following code will make your TextView selectable.

package com.zyz.mobile.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {

    private SelectableTextView mTextView;
    private int mTouchX;
    private int mTouchY;
    private final static int DEFAULT_SELECTION_LEN = 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // make sure the TextView's BufferType is Spannable, see the main.xml
        mTextView = (SelectableTextView) findViewById(R.id.main_text);
        mTextView.setDefaultSelectionColor(0x40FF00FF);


        mTextView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showSelectionCursors(mTouchX, mTouchY);
                return true;
            }
        });
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.hideCursor();
            }
        });
        mTextView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mTouchX = (int) event.getX();
                mTouchY = (int) event.getY();
                return false;
            }
        });
    }

    private void showSelectionCursors(int x, int y) {
        int start = mTextView.getPreciseOffset(x, y);

        if (start > -1) {
            int end = start + DEFAULT_SELECTION_LEN;
            if (end >= mTextView.getText().length()) {
                end = mTextView.getText().length() - 1;
            }
            mTextView.showSelectionControls(start, end);
        }
    }
}
Penutian answered 16/6, 2013 at 19:50 Comment(4)
dear Ray Zhou, i have problem while facing JTextView inside scrollView that have paddingTop attribute, i think you need to add padding offset in getScrollXInternal() in JTextView. correct me if i'am wrong, thanksLicastro
Hi, @Licastro I have made a couple of fix to the JTextView since I posted this, but the fixes are in BitBucket, not GitHub. You could take a look there.Penutian
It just work inside scrollView :( . how can i change it to work inside layout?Abigael
@RayZhou Do you still have a public repo for Jade Reader? The BitBucket one seems to have bit the bucket.Limb
B
10

It depends on the minimum Android version that you'd like to support.

On 3.0+, you have the textIsSelectable attribute on the TextView, which enables this behavior. E.g.:

<TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        android:bufferType="spannable"
        android:textIsSelectable="true"
        android:textSize="28dip"
        tools:context=".MainActivity" />

Below that, you best bet is to use an EditText that looks and behaves like a TextView (apart from the slection thing). Or you can implement this feature yourself using spans.

Biggerstaff answered 10/9, 2012 at 8:54 Comment(3)
But if u see screenshot i posted which is of moon+ reader.. and they have that feature.. So what u think ? do they are using edittext for that ?Gastroscope
My bet is that they used EditText for that. Try using HierarchyViewer, it may reveal this. Or try asking the devs :)Southpaw
This is obviously the way to go.Angelitaangell

© 2022 - 2024 — McMap. All rights reserved.