0

I would like to have a TextView such that the user can select/copy the text from the TextView. The TextView is part of a Fragment in a TabLayout. The Fragment has the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">

    <TextView
        android:id="@+id/textview_translator_text"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/background_default"
        android:textIsSelectable="true"
        android:textSize="@dimen/edittext_textsize_translator"
        android:padding="@dimen/edittext_padding_translator"
        android:scrollbars="vertical"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/view_height_spacing" />

    <EditText
        android:importantForAutofill="no"
        android:inputType="text"
        android:id="@+id/edittext_translator_text"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/background_default"
        android:hint="@string/type_text"
        android:gravity="top"
        android:textSize="@dimen/edittext_textsize_translator"
        android:padding="@dimen/edittext_padding_translator"
        android:scrollbars="vertical"/>

</LinearLayout>

And the corresponding Java code is as follows:

public class FragmentText extends Fragment {

    private TextView textView;

    private View rootView;

    private TextTranslator textTranslator;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_text, container, false);

        textTranslator = new TextTranslator();

        initializeTextView();
        initializeEditText();

        return rootView;
    }

    private void initializeTextView() {
        textView = rootView.findViewById(R.id.textview_translator_text);

        textView.setMovementMethod(new ScrollingMovementMethod());
    }

    private void initializeEditText() {
        EditText editText = rootView.findViewById(R.id.edittext_translator_text);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // currently not used
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String textInBinary = textTranslator.getBinarySequence(charSequence.toString());

                textView.setText(textInBinary);
            }

            @Override
            public void afterTextChanged(Editable editable) {
                // currently not used
            }
        });
    }
}

I expected that the text of the TextView is selectable (since I am using the property textIsSelectable). However, for some reason, the text is not selectable.

I've tested the application on multiple devices with different Android versions, and on none of these devices the text was selectable.

Any suggestion how I can make the text of the TextView selectable?

tomhogenkamp
  • 83
  • 1
  • 11
  • Thats strange indeed, as setting ```android:textIsSelectable="true"``` should work. Are you setting some listeners on the textview in your java class – ljk Feb 21 '20 at 10:45

1 Answers1

3

Somehow reached back at this post. Looking at the edit you made to your question the cause of your issue seems to be this line

textView.setMovementMethod(new ScrollingMovementMethod());

inside your initializeTextView() method.

In simple terms its overwriting what you set in the xml with android:textIsSelectable="true" You can read this SO post for some more info of how it's causing the issue.

Simplest solution is to remove the scrolling movement method from code and use android:textIsSelectable="true" in xml or set TextView as selectable programatically

textView.setTextIsSelectable(true);

as it seems to set values of some other properties like focusable, focusableInTouchMode, longClickable to true too.

ljk
  • 1,496
  • 1
  • 11
  • 14