.setBackgroundColor with Hex Color Codes AndroidStudio
There are two main classes for color handling in Java/Android.
This first one is from "plain" Java and can be found in java.awt.Color
.
This class supports converting a String into a color with the method decode.
Example:
Color red = Color.decode("#FF0000");
The second class is for Android and can be found in android.graphics.Color
.
The conversion can be done with the method parseColor.
int red = Color.parseColor("#FF0000");
So you should check which kind of Color
class you've imported to your project. I recommend using the Android version of Color for your case. If you've done that the statement targetView.setBackgroundColor(Color.parseColor("#FFFFFF"));
should work.
how to set hex color code for background
I like to use this little piece of code to use HTML web colors in my apps.
Usage:
[self.view setBackgroundColor: [self colorWithHexString:@"FFFFFF"]]; /* white */
The Code:
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
Set Background Color using HEX
I would suggest .attr( attributeName, function ) and CSSStyleDeclaration.removeProperty() instead to use a regex:
$("div").attr('style', function() {
this.style.removeProperty('color'); // remove color if exist....
return this.style.cssText + 'color:#3282c3;'; // add your color
});
$("div").each(function() {
console.log(this.outerHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="font-family: Helvetica;line-height: 100%;margin-top: 20px; text-align: left;vertical-align: bottom;color: blue">
I want a hex color! (I have already a style prop...)
</div>
<div>
I want a hex color!
</div>
How to change desired div's background color by entering hex color in input filed (with jQuery or without)?
JQuery
you can use keyup
event:
$('.pickedColor').keyup(function() {
$('.elementToChange').css('background-color', $(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div>
<span>Enter color (Hex Code):</span>
<span> <input class="pickedColor" type="text"/> </span>
</div>
</br>
<div class="elementToChange" style="height:40px; width:40px; border:1px solid black"></div>
how to set a view's background color using a precise hex via interfacebuilder?
Add a hex color picker to your system. I use http://wafflesoftware.net/hexpicker/.
for loop print same hex color as background color
You could clean it up a bit and use the same color for the background color too.
function returnMe() {
var content = "";
for (var i = 0; i < 5; i++) {
var color = '#' + (function co(lor){ return (lor +=
[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'][Math.floor(Math.random()*16)])
&& (lor.length == 6) ? lor : co(lor); })('');
content += "<a class='a' href=''>";
content += "<span class='b'>";
content += color + "</span>";
content += "<span class='c' style='background: "+ color +"'></span>";
content += "</a>";
}
document.getElementById("clr").innerHTML = content;
}
returnMe();
.a {
display: inline-block;
background: #fff;
border: 1px solid #e2e2e2;
width: 180px;
height: 180px;
margin: 10px;
text-align: center
}
.b {
background: #f9f9f9;
display: inline-block;
width: 100%;
padding: 10px 0
}
.c {
display: inline-block;
width: 100%;
height: 141px
}
<div id="clr"></div>
Get Background Color of a View in hex
LinearLayout layout = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) layout.getBackground();
int colorId = viewColor.getColor();
After getting as integer type of color, now you have to convert to hexadecimal:
String hexColor = String.format("#%06X", (0xFFFFFF & colorId));
Hope this helps..
How can I get HEX or RGB color code of the window background color?
The winfo_rgb
method on all widgets will accept a color name and return the r, g, and b components as integers in the range of 0-65535 (16 bits). You can then convert those to hex using standard python string formatting.
Related Topics
Swift Formatting String to Have 2 Decimal Numbers If It Is Not a Whole Number
Wkwebview Fails to Load Images and CSS Using Loadhtmlstring(_, Baseurl:)
How Do We Create a Bigger Center Uitabbar Item
What #Defines Are Set Up by Xcode When Compiling for Iphone
Show iPhone Cut Copy Paste Menu on Uilabel
Tap on a Part of Text of Uilabel
Ios7 Status Bar Hide/Show on Select Controllers
How to Programmatically Add a Proxy to an Nsurlsession
How to Make the View Update Instant in Swiftui
How to Prevent iOS 13 Dark Mode from Breaking Emails
Sort Array of Dictionaries by Key Value
How to Display the Default iOS 6 Share Action Sheet with Available Share Options
Silent Push Notifications Only Delivered If Device Is Charging And/Or App Is Foreground
Refresh Uipageviewcontroller - Reorder Pages and Add New Pages
How to Draw Radial Gradients in a Calayer
How to Set Cmake_C_Compiler and Cmake_Cxx_Compiler for Building Assimp for iOS