1

I have created fragments inside an activity. Each fragment have a textview. I want to make all the textviews inside the fragments to be scroll-able. Below is my xml and java code. Thanks in advance

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chemistry.AtomicNumber">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/atomicNumberDefinition"
            android:text="The atomic number..... "
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp"
            android:scrollbars="vertical" />

Java Code:

public class AtomicNumber extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public AtomicNumber() {
    }

    public static AtomicNumber newInstance(String param1, String param2) {
        AtomicNumber fragment = new AtomicNumber();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_atomic_number, container, false);
    }
}

Please let me know how to write a proper code in the java file.

hata
  • 11,633
  • 6
  • 46
  • 69

3 Answers3

4

I would suggest to use ScrollView:

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/atomicNumberDefinition"
        android:text="The atomic number..... "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="24sp" />
</ScrollView>
hata
  • 11,633
  • 6
  • 46
  • 69
0

You can using ScrollView or NestedScrollView. Beside RecycleView can using if you need scroll item of list.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 10:26
0

In your Fragment, access your TextView and set its MovementMethod to ScrollingMovementMethod.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  final TextView textView = view.findViewById(R.id.atomicNumberDefinition);
  textView.setMovementMethod(new ScrollingMovementMethod());
}
Dannly
  • 341
  • 1
  • 4