2

I have colors in my String.xml file, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<resources>


      <string name="txtEmail">Email </string>
       <string name="txtPin">Pin Code</string>
       <string name="lblProtected"> Protected</string>

       <color name="MiscellaneousPercent">#ffff992b</color>
       <color name="MusicPercent">#ffffdd58</color>
       <color name="PicturesPercent">#ff48aa71</color>
       <color name="eBooksPercent">#ff00cff9</color>
       <color name="DocumentsPercent">#ff019df2</color>
</resources>

I want to use the these colors in class in my projects in order to iterate through the colors, by code.

    public class BreakDownBar extends View {

        public BreakDownBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }


            @Override
            protected void onDraw(Canvas canvas) {

                     for (int i = 0; i < 5; i++) {
            Paint paint = new Paint();
                        paint.setColor(/*I want to use the colors HERE*/); 
            }


                }
}

How can I use the colors in the onDraw method in SetColor()like above? Can I put the colors in the String.XMl file in an array? Can any one help me?

3 Answers 3

4

Use like this

context.getResources().getColor(R.color.MusicPercent);
Sign up to request clarification or add additional context in comments.

Comments

3

You have to make use of your Context, and provide your resource in a array format and use it.

public class BreakDownBar extends View {

    Context context=null;
int res[]={R.color.black,R.color.blue,R.color.white,R.color.pink,R.color.grey};

        public BreakDownBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context=context;
        }


            @Override
            protected void onDraw(Canvas canvas) {

                     for (int i = 0; i < 5; i++) {
                       Paint paint = new Paint();
                       paint.setColor(res[i]));
            }


                }
}

3 Comments

i know that, what i want is that the color will iterate across the colors i have. in code you gave me the paint will have the black colors always!!
i want to put the colors in the String.xml in a n array
yeah.. cool. If u had asked for the iteration process at the earlier it wud have been more easier.. not a problem though. happy that it helped you.. keep going..
1

//you can use by get resource

paint.setColor(context.getResources().getColor(R.color.custom_red)); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.