Search code examples
androidcolorsdrawable

Change color of drawable xml files progmatically without findViewById()


enter image description here

So what i want to do is..

  1. Get the drawable shape(xml file)
  2. Get drawable's color
  3. Change it's color using colors available in colors.xml
  4. Set that drawable to some ImageView on CardView(that's not the issue)

Here are the codes:-

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="md_red_900">#B71C1C</color>
    <color name="md_pink_900">#880E4F</color>
    <color name="md_purple_900">#4A148C</color>
    <color name="md_deep_purple_900">#311B92</color>
    ...
    <!--15 more colors-->
</resources>

shape_template.xml(drawable shape):-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/md_white_1000" />
        </shape>
    </item>
</selector>

MainColorPaletteAdapter.java(where i want change color of drawable):-

package com.danish.foveros.adapter;

import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.danish.foveros.R;  
import java.util.ArrayList;
import java.util.List;

public class MainColorPaletteAdapter extends RecyclerView.Adapter<MainColorPaletteAdapter.ViewHolder> {
    List<MainColorPaletteGetSet> mItems;
    String[] colorName;
    int[] colorThumbnail;
    int[] allMaterialColor900;
    int[] colorNameBackgroundColor;

    public MainColorPaletteAdapter() {
        super();
        mItems = new ArrayList<MainColorPaletteGetSet>();
        MainColorPaletteGetSet color;

        colorName = new String[]{"Amber", "Brown", "Cyan", "Teal"};
        colorThumbnail = new int[]{R.drawable.amber, R.drawable.brown,
                R.drawable.cyan, R.drawable.teal};
        colorNameBackgroundColor = new int[]{R.color.md_amber_900, R.color.md_brown_900,
                R.color.md_cyan_900, R.color.md_teal_900};

        for (int i = 0; i < colorName.length; i++) {
            color = new MainColorPaletteGetSet();
            color.setColorName(colorName[i]);
            color.setColorThumbnail(colorThumbnail[i]);
            color.setColorNameBackgroundColor(colorNameBackgroundColor[i]);
            mItems.add(color);
        }    
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater
                .from(viewGroup.getContext())
                .inflate(R.layout.fragment_home_data, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MainColorPaletteGetSet color = mItems.get(position);
        holder.colorThumbnail.setImageResource(color.getColorThumbnail());
        holder.colorName.setText(color.getColorName());
        holder.colorName.setBackgroundColor(color.getColorNameBackgroundColor());

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView colorThumbnail;
        public TextView colorName;

        public ViewHolder(View itemView) {
            super(itemView);
            colorThumbnail = (ImageView) itemView.findViewById(R.id.colorThumbnail);
            colorName = (TextView) itemView.findViewById(R.id.colorName);

        }
    }
}

I have tried many methods but they say to get the drawable or the background color of View by findViewById() but it's not working for me.

As you can see drawable shape is a very simple shape so it might not be difficult to do that.

I thought instead creating 19 drawable xml files i can get one drawable change it's color use it for first cardview change it's color and use it for second cardview and so on.

Ask any more information if you need.

Any help is appreciated. Thank you.


Solution

  • Solved it myself:-

    package com.danish.foveros.adapter;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.media.Image;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    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 com.danish.foveros.R;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainColorPaletteAdapter extends RecyclerView.Adapter<MainColorPaletteAdapter.ViewHolder> {
        List<MainColorPaletteGetSet> mItems;
        String[] colorName;
        int[] colorThumbnail;
        int[] allMaterialColor900;
        int[] colorNameBackgroundColor;
    
        public MainColorPaletteAdapter() {
            super();
            mItems = new ArrayList<MainColorPaletteGetSet>();
            MainColorPaletteGetSet color;
            colorNameBackgroundColor = new int[]{
                    R.color.md_red_900,
                    R.color.md_pink_900,
                    R.color.md_purple_900,
                    R.color.md_deep_purple_900,
                    R.color.md_indigo_900,
                    R.color.md_blue_900,
                    R.color.md_light_blue_900,
                    R.color.md_cyan_900,
                    R.color.md_teal_900,
                    R.color.md_green_900,
                    R.color.md_light_green_900,
                    R.color.md_lime_900,
                    R.color.md_yellow_900,
                    R.color.md_amber_900,
                    R.color.md_orange_900,
                    R.color.md_deep_orange_900,
                    R.color.md_brown_900,
                    R.color.md_grey_900,
                    R.color.md_blue_grey_900
            };
    
            colorName = new String[]{
                    "Red",
                    "Pink",
                    "Purple",
                    "Deep Purple",
                    "Indigo",
                    "Blue",
                    "Light Blue",
                    "Cyan",
                    "Teal",
                    "Green",
                    "Light Green",
                    "Lime",
                    "Yellow",
                    "Amber",
                    "Orange",
                    "Deep Orange",
                    "Brown",
                    "Grey",
                    "Blue Grey"
            };
            colorThumbnail = new int[]{
                    R.color.md_red_500,
                    R.color.md_pink_500,
                    R.color.md_purple_500,
                    R.color.md_deep_purple_500,
                    R.color.md_indigo_500,
                    R.color.md_blue_500,
                    R.color.md_light_blue_500,
                    R.color.md_cyan_500,
                    R.color.md_teal_500,
                    R.color.md_green_500,
                    R.color.md_light_green_500,
                    R.color.md_lime_500,
                    R.color.md_yellow_500,
                    R.color.md_amber_500,
                    R.color.md_orange_500,
                    R.color.md_deep_orange_500,
                    R.color.md_brown_500,
                    R.color.md_grey_500,
                    R.color.md_blue_grey_500
            };
    
    
            for (int i = 0; i < colorName.length; i++) {
                color = new MainColorPaletteGetSet();
                color.setColorName(colorName[i]);
                color.setColorThumbnail(colorThumbnail[i]);
                color.setColorNameBackgroundColor(colorNameBackgroundColor[i]);
                mItems.add(color);
            }
    
    
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View v = LayoutInflater
                    .from(viewGroup.getContext())
                    .inflate(R.layout.fragment_home_data, viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(v);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            MainColorPaletteGetSet color = mItems.get(position);
            holder.colorThumbnailView.setBackgroundResource(color.getColorThumbnail());
            holder.colorNameView.setText(color.getColorName());
            holder.colorNameView.setBackgroundResource(color.getColorNameBackgroundColor());
        }
    
        @Override
        public int getItemCount() {
            return mItems.size();
        }
    
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            public ImageView colorThumbnailView;
            public TextView colorNameView;
    
    
    
            public ViewHolder(final View itemView) {
                super(itemView);
                colorThumbnailView = (ImageView) itemView.findViewById(R.id.colorThumbnail);
                colorNameView = (TextView) itemView.findViewById(R.id.colorName);
            }
    
        }
    }
    

    Final output:-

    enter image description here