{"id":5722,"date":"2014-04-06T10:29:31","date_gmt":"2014-04-06T10:29:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/how-to-programmatically-round-corners-and-set-random-background-colors-collection-of-common-programming-errors\/"},"modified":"2014-04-06T10:29:31","modified_gmt":"2014-04-06T10:29:31","slug":"how-to-programmatically-round-corners-and-set-random-background-colors-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/how-to-programmatically-round-corners-and-set-random-background-colors-collection-of-common-programming-errors\/","title":{"rendered":"How to programmatically round corners and set random background colors-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;d like to round the corners of a view and also change the color of the view based on the contents at runtime.<\/p>\n<pre><code>                TextView v = new TextView(context);\n                v.setText(tagsList.get(i));\n\n                if(i%2 == 0){\n                    v.setBackgroundColor(Color.RED);\n                }\n                else{\n                    v.setBackgroundColor(Color.BLUE);\n                }\n\n                v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));\n                v.setPadding(twoDP, twoDP, twoDP, twoDP);\n                v.setBackgroundResource(R.drawable.tags_rounded_corners);\n<\/code><\/pre>\n<p>I was hoping setting a drawable and color would overlap, but they do not. Whichever one I execute second is the resulting background.<\/p>\n<p>Is there any way to programmatically create this view, keeping in mind that the background color won&#8217;t be decided until runtime?<\/p>\n<p>edit: I&#8217;m only swapping between red and blue now for testing. Later the color will be choosable by the user.<\/p>\n<p>edit:<\/p>\n<p>tags_rounded_corners.xml:<\/p>\n<pre><code>\n\n    \n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;d like to round the corners of a view and also change the color of the view based on the contents at runtime. TextView v = new TextView(context); v.setText(tagsList.get(i)); if(i%2 == 0){ v.setBackgroundColor(Color.RED); } else{ v.setBackgroundColor(Color.BLUE); } v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); v.setPadding(twoDP, twoDP, twoDP, twoDP); v.setBackgroundResource(R.drawable.tags_rounded_corners); I was hoping setting a drawable and color would overlap, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5722","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5722"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5722\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}