dimanche 11 décembre 2016

Generate and set random colors on text view background within Recycler View in Android

I am Trying to Generate Random Colors and set the Random color as background of Text View Just Like in GMail app. The Text view is Having a circular background initially set in xml which i have done using shape. I have done some research and used some code available on internet but the changes are not reflecting in my app. Gmail

Below is my Recycler View Adapter Class:

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Items> {
        ArrayList<GmailDataHolder> data;
        Context context;

        public RecyclerViewAdapter(ArrayList<GmailDataHolder> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public RecyclerViewAdapter.Items onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.gmail_layout_row, parent, false);
            Items i = new Items(v);
            return i;


        }

        @Override
        public void onBindViewHolder(final RecyclerViewAdapter.Items holder, int position) {

//Generating Random Color
            int randomAndroidColor = holder.androidColors[new Random().nextInt(holder.androidColors.length)];
            Drawable background = holder.circleTv.getBackground();
            if (background instanceof ShapeDrawable) {
                ((ShapeDrawable)background).getPaint().setColor(randomAndroidColor);
            } else if (background instanceof GradientDrawable) {
                ((GradientDrawable)background).setColor(randomAndroidColor);
            } else if (background instanceof ColorDrawable) {
                ((ColorDrawable)background).setColor(randomAndroidColor);
            }
            holder.line1.setText(data.get(position).getLine1());
            holder.line2.setText(data.get(position).getLine2() + "...");
            holder.line3.setText(data.get(position).getLine3() + "...");
            holder.time.setText(data.get(position).getTime());


            //get Star Image State from MySql DB

            MyFunctions.getStarState(data, holder, position);
    holder.circleTv.setText(String.valueOf(data.get(position).getLine1().charAt(0)).toUpperCase());

            //Changing Star Image on Click
            MyFunctions.starClickListener(holder);


        }

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

        public class Items extends RecyclerView.ViewHolder {
            TextView circleTv, line1, line2, line3, time;
            int[] androidColors;
            public ImageView star;

            public Items(View itemView) {
                super(itemView);
//Loading Color from resources
                androidColors = itemView.getResources().getIntArray(R.array.androidcolors);
                circleTv = (TextView) itemView.findViewById(R.id.tv_circle);
                line1 = (TextView) itemView.findViewById(R.id.tv_line1);
                line2 = (TextView) itemView.findViewById(R.id.tv_line2);
                line3 = (TextView) itemView.findViewById(R.id.tv_line3);
                time = (TextView) itemView.findViewById(R.id.tv_time);
                star = (ImageView) itemView.findViewById(R.id.img_star);


            }
        }

Colors.xml File:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#dc4538</color>
    <color name="colorFacebook">#374dae</color>
    <color name="colorCenterFb">#d5617ae6</color>
    <color name="colorStartGoogle">#d8dd4c3a</color>
    <color name="colorEndGoogle">#dd4c3a</color>
    <color name="colorEndLinkedIn">#1887b0</color>
    <color name="colorStartLinkedIn">#e31887b0</color>
    <color name="colorStrokeLinkedIn">#ec106584</color>
    <color name="colorStrokeGoogle">#b73e2e</color>
    <color name="colorStrokeFacebook">#e2263a91</color>
    <color name="status">#ba3223</color>
    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

    <integer-array name="androidcolors">
        <item>@color/blue</item>
        <item>@color/purple</item>
        <item>@color/green</item>
        <item>@color/orange</item>
        <item>@color/red</item>
        <item>@color/darkblue</item>
        <item>@color/darkpurple</item>
        <item>@color/darkgreen</item>
        <item>@color/darkorange</item>
        <item>@color/darkred</item>
    </integer-array>


</resources>

Initially The TextView is having the following Background declared in xml:

<shape xmlns:android="http://ift.tt/nIICcg"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>




Aucun commentaire:

Enregistrer un commentaire