android - How can I get the colors hex code from custom colors.xml that I used on a View object? -


so have custom colors.xml:

<color name="yellow">#ffff00</color> <color name="pink">#ff00ff</color> <color name="red">#ff0000</color> .................... 

if used color on object (i.e. view) color background how can hex code in tags? mean colored view yellow , know hex code has object -> #ffff00

how can that?

relativelayout re=(relativelayout)rootview.findviewbyid(r.id.idstack); colordrawable colordrawable=(colordrawable)re.getbackground(); string hexcolor=integer.tohexstring(colordrawable.getcolor()); 

hexcolor -> ffff00

update

in order name of color (#ff0000->red), assuming you're not doing complicated since provided color.xml example in question should this:

//create map of colors you'll need map<integer,string> mycolors=new hashmap<integer, string>(){  {     //color constants @ http://developer.android.com/reference/android/graphics/color.html#black     put(-16777216,"black");     put(-256,"yellow");     ...and on...   } };  //get color name map string colorname=mycolors.get(color.parsecolor("#"+hexcolor)); //colorname -> yellow     

otherwise since above snippet finds exact match, try finding closest color using best match algorithm 1 example: best algorithm matching colours.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -