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?