Android: unable to set background drawable color to percentRelativeLayout

410 Views Asked by At

I am using view tag inside PercentRelativeLayout. I need to set background color to respective tag which I am unable to do programmatically.

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/leaderboardRowRelativeLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

    <View
        android:id="@+id/percentile_scored"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="15%"
        />

    <View
        android:id="@+id/percentile_left_scoring"
    android:layout_width="0dp"
    android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="15%"
        android:layout_toRightOf="@+id/percentile_scored"
    />

The place where I try set background:

  if(String.valueOf(userId).equals(leaderBoardDb.getUserId())){
                leaderboardFriendsViewHolder.percent_scored.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.leaderboard_user_color));

            }else {
                leaderboardFriendsViewHolder.percent_scored.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.leaderboard_row_color));
            }
1

There are 1 best solutions below

3
On

Well I tried same code as you.

1) In design:

<android.support.percent.PercentRelativeLayout
    android:id="@+id/leaderboardRowRelativeLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/percentile_scored"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="50%"
        />

    <View
        android:id="@+id/percentile_left_scoring"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@+id/percentile_scored"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="50%"
        />
</android.support.percent.PercentRelativeLayout>

2) In RecyclerAdapter:

...
@Override
    public void onBindViewHolder(ProductImagesRecyclerAdapter.ViewHolder holder, int position) {
        Timber.e("Position: " + position);
        if(position%2 == 0) {
            holder.aa.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
        } else {
            holder.aa.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
        }
    }
...

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        int position;
        View aa;


        public ViewHolder(View v, final ProductImagesRecyclerInterface productImagesRecyclerInterface) {
            super(v);
            aa = v.findViewById(R.id.percentile_scored);
        }

        public void setPosition(int position) {
            this.position = position;
        }

    }

And result seems totaly OK: enter image description here

Problem is probably somewhere else. Maybe views width and height are zeros or some other views overlap them.