Android DarkMode textColor not changed inside RecyclerView's row file

Viewed 939

I have set the recycler view and set one textview with simple text in row file of RecyclerView.

I have set the fragment according to tab mean I have used the BottomNavigationView and there is one fragment which contains the RecyclerView.

And I have set the android:forceDarkAllowed="true" inside the xml file.

I have applied the DayNight theme.

The color of the textview which is inside the fragment's xml can be update according to dark and light theme, but the adapter's item(Used for the RecyclerView) which contains the textview that not changed the color.

Pref. XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="@dimen/margin_medium"
    android:forceDarkAllowed="true"
    android:orientation="vertical">

    <TextView
        style="?attr/textAppearanceHeadline6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_medium"
        android:textColor="@color/txt"
        android:text="@string/preferences"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Fragment:


public class PreferencesFragment extends Fragment {

    static final String TAG = "PreferencesFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_preferences, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerView rvList = view.findViewById(R.id.rvList);

        rvList.setLayoutManager(new LinearLayoutManager(DarkThemeApplication.context, RecyclerView.VERTICAL, false));
        rvList.setAdapter(new RVListAdapter());
    }

    class RVListAdapter extends RecyclerView.Adapter<RVListAdapter.ViewHolder> {

        @NonNull
        @Override
        public RVListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
            int viewType) {
            return new ViewHolder(LayoutInflater
                .from(DarkThemeApplication.context).inflate(R.layout.row_txt, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RVListAdapter.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 50;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }

}

Row File XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:forceDarkAllowed="true">

    <TextView
        android:id="@+id/switch1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/txt"
        android:layout_margin="@dimen/margin_small"
        android:text="asdasdasdas" />

</LinearLayout>
1 Answers
LayoutInflater
            .from(DarkThemeApplication.context).inflate(R.layout.row_txt, ...

Use an Activity context and not an Application one for inflating your views.

Your adapter is a non-static inner class in a Fragment so you can just replace DarkThemeApplication.context with requireActivity().

Related