Dark mode color not resolved correctly in RecyclerView

goraga1 :

I am trying to implement night mode, but I have issues: CardView doesn't change color to night value but activity correctly changes its background to night color values.

Please help to understand the issue.

MainActivity

public class NewFinishActivity extends AppCompatActivity {

    @BindView(R.id.finishRecycler)
    RecyclerView finishRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

        setContentView(R.layout.activity_new_finish);
        ButterKnife.bind(this);


        LevelRecyclerAdapter mAdapter = new LevelRecyclerAdapter(getApplicationContext(), new ArrayList<>());
        finishRecycler.setAdapter(mAdapter);
    }
}

MainActivity layout

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/background_color"
    tools:context=".activities.NewFinishActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/finishRecycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

Adapter

public class LevelRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<UserLevel> mItems;
    private Context mContext;

    public LevelRecyclerAdapter(Context context, List<UserLevel> items) {
        mContext = context;
        mItems = items;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AbstractCard card;
        card = new LevelCardView(mContext, parent);
        return card;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (!mItems.isEmpty()) {
            UserLevel data = mItems.get(position);
            LevelCardView card = (LevelCardView) viewHolder;
            card.bind(data);
        }
    }

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

    public void update(List<UserLevel> items) {
        mItems = items;
        notifyDataSetChanged();
    }
}

RecyclerView.ViewHolder

public class LevelCardView extends AbstractCard {

    Context mContext;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.levelCheck)
    AppCompatImageView levelCheck;
    @BindView(R.id.levelCard)
    CardView levelCard;

    public LevelCardView(Context context, ViewGroup parent) {
        this(context, LayoutInflater.from(context).inflate(R.layout.card_level, parent, false));
    }

    private LevelCardView(Context context, View view) {
        super(view, context);
        mContext = context;
        try {
            ButterKnife.bind(this, view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

RecyclerView.ViewHolder xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/levelCard"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="@color/background_color_card"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="@dimen/card_evalation">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:padding="15dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:drawableLeft="@drawable/ic_goal_1"
                android:fontFamily="@font/roboto"
                android:gravity="center"
                android:text="Lose Fat"
                android:textColor="@color/textColorStandard"
                android:textSize="20dp" />

        </RelativeLayout>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/levelCheck"
            android:layout_width="70dp"
            android:visibility="gone"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/gender_selected" />
    </FrameLayout>

</androidx.cardview.widget.CardView>

colors.xml

<color name="textColorStandard">#282b35</color>
<color name="background_color">#fdfdfdfd</color>
<color name="background_color_card">#fff</color>
<color name="textColorStandardTransparent">#B222273D</color>

colors.xml NIGHT

<color name="textColorStandard">#FCFCFC</color>
<color name="background_color">#14182A</color>
<color name="background_color_card">#2F3550</color>
<color name="textColorAlwaysStandard">#282b35</color>
azizbekian :

You are using incorrect context. Instead of using context of the application you should be using context of the activity.

Instead of performing:

card = new LevelCardView(mContext, parent);

Perform following:

card = new LevelCardView(parent.getContext(), parent);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=336746&siteId=1