Dark Theme not applying in some recyclerview items

Viewed 575

I have an app with multiple activities and fragments, and inside them there are recycler views. On one of my fragments, the recycler view items change to dark theme automatically, just like they should. But when using the exact same adapter class and exact same item layout on a different activity, it not longer applies the dark theme.

Item Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

        <androidx.cardview.widget.CardView
            android:id="@+id/crv_anime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground"
            android:clipToPadding="false"
            android:layout_marginBottom="14dp"
            app:cardCornerRadius="10dp"
            android:layout_marginTop="28dp"
            app:cardElevation="10dp"
            android:layout_centerHorizontal="true">

            <RelativeLayout
                android:layout_width="550px"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/img_anime_cover"
                    android:layout_width="550px"
                    android:layout_height="777px"
                    android:layout_centerHorizontal="true"/>

                <TextView
                    android:id="@+id/lbl_anime_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/img_anime_cover"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:textAlignment="center"
                    android:fontFamily="@font/segoeb"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:textSize="16sp"/>

            </RelativeLayout>
        </androidx.cardview.widget.CardView>
</RelativeLayout>

Adapter Class:

package com.animewatcher.animewatcher;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {

    List<anime_class> mAnime;
    Context mContext;
    LayoutInflater inflater;

    public RecycleAdapter(Context context, List<anime_class> anime){
        this.mAnime = anime;
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecycleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.grid_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecycleAdapter.ViewHolder holder, int position) {
            holder.title.setText(capitalizeLetters(mAnime.get(position).getmNameEN()));
            Glide.with(mContext).load(mAnime.get(position).getmThumbnail()).into(holder.thumbnail);

            holder.crv_.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(mContext, anime_page.class);
                            i.putExtra("mNameEN", mAnime.get(position).getmNameEN());
                            i.putExtra("mThumbnail", mAnime.get(position).getmThumbnail());
                            mContext.startActivity(i);
                        }
                    }
            );
    }

    @Override
    public int getItemCount() {
        return mAnime.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        ImageView thumbnail;
        CardView crv_;

        public ViewHolder(@NonNull View itemView){
            super(itemView);
            title = itemView.findViewById(R.id.lbl_anime_title);
            thumbnail = itemView.findViewById(R.id.img_anime_cover);
            crv_ = itemView.findViewById(R.id.crv_anime);
        }
    }

    private String capitalizeLetters(String str)
    {
        String[] strArray = str.split(" ");
        StringBuilder builder = new StringBuilder();
        for (String s : strArray) {
            String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
            builder.append(cap + " ");
        }

        return builder.toString();
    }
}

Fragment code (where the dark theme is working):

if (getActivity()!=null) {
                        if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                            adapter = new RecycleAdapter(getActivity(), animes);
                            GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
                            animeList.setLayoutManager(gridLayoutManager);
                            animeList.setAdapter(adapter);
                        }
                        else
                        {
                            adapter = new RecycleAdapter(getActivity(), animes);
                            GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
                            animeList.setLayoutManager(gridLayoutManager);
                            animeList.setAdapter(adapter);
                        }
                    }

Activity code (where dark theme is not working):

if (getApplication()!=null) {
                            if(getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                                adapter = new RecycleAdapter(getApplicationContext(), animes);
                                GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 3, GridLayoutManager.VERTICAL, false);
                                animeList.setLayoutManager(gridLayoutManager);
                                animeList.setAdapter(adapter);
                            }
                            else
                            {
                                adapter = new RecycleAdapter(getApplicationContext(), animes);
                                GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2, GridLayoutManager.VERTICAL, false);
                                animeList.setLayoutManager(gridLayoutManager);
                                animeList.setAdapter(adapter);
                            }
                        }

Image of the dark theme correctly applied Image of the item not applying dark theme

2 Answers

I think I've got your problem. On the second code provided (Dark theme not applied), you've used getApplicationContext() which is not ActivityContext aware, and so theme not applied properly. Use Activity context instead (getActivity() if inside fragment or Activity.this if inside Activity) like the first code.

adapter = new RecycleAdapter(getActivity(), animes);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);

possible solutions:

1- check theme applied to that activity which is not showing items in dark mode. 2- check if you set background color to white in that activity main layout or recyclerview.

Your code seems fine.

Related