Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:08:41+00:00 2026-06-17T16:08:41+00:00

If i have an xml file with custom colors like this in res/values/colors: <?xml

  • 0

If i have an xml file with custom colors like this in res/values/colors:

<?xml version="1.0" encoding="utf-8"?> 
<resources>
 <drawable name="red">#ff0000</drawable>
 <drawable name="blue">#0000ff</drawable>
 <drawable name="green">#00ff00</drawable>
</resources>

How can I use colors or other values from it in other code?

How can I use these for arguments? Something like:

    int green = context.getResources().getColor(R.color.green);
    g.drawRect(1, 1, 181, 121, green);

gives errors in logcat and crashes the program. So if colors.xml is in res/values/
and I have context imported how can I use green, for example in an argument?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:08:41+00:00Added an answer on June 17, 2026 at 4:08 pm

    First, change drawable to color in your xml.

    Then you need to have context. It goes like this:

    context.getResources().getColor(R.color.green);

    It returns an int color value.

    Edit:

    For other values, see the functions here:

    http://developer.android.com/reference/android/content/res/Resources.html

    I like tp get all my xml colors once and pass them around from there so I am not typing the above over and over. Not sure if this is considered best practice.

    If you want to use this in a Paint, it could be:

    // Declare this at the beginning:
    Paint green paint;
    // This goes in the constructor:
    greenPaint = new Paint();
    greenPaint.setColor(context.getResources().getColor(R.color.green));
    // then draw something in onDraw, for example:
    canvas.drawRect(5,5,5,5, greenPaint);
    

    If you want to use it in multiple Paints, etc. save it as an int:

    int greenNum = context.getResources().getColor(R.color.green);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom spinner dropdown xml file in /res/layout/: spinner_view_dropdown.xml: <?xml version=1.0 encoding=UTF-8?>
I have this XML file: <?xml version=1.0 encoding=utf-8?> <Wpf> <wpfctrl id=NavBtnTemplate>#FFCFDEFF</wpfctrl> </Wpf> and i
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>
I have some data in an xml file put it in /res/values/mydata.xml. I want
I have created custom listview, each row looks like my file custom_row.xml. Is there
Good day I have a custom TextBox that has a IndicatorTextBox.ui.xml file as well
I am currently watching an XML file from log4j output. I have a custom
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have custom component that I can place in my layout file (XML) for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.