how to check String is valid hex color code in android
android, java
Solution
The proper Android way of testing this, independently of supporting future formats, is to rely on the `Color` class.
Like this:
try {
Color color = Color.parseColor(myColorString);
// color is a valid color
} catch (IllegalArgumentException iae) {
// This color string is not valid
}
As a bonus, it also supports named colors such as `magenta`, `blue`...
The main pro over regex matching is semantic. This code explicitly tests the `myColorString` String against being a Color. You practically don't need any comment at all to tell what is does.
Problem
I had a String which I need to validate whether it is valid hex color code or not . My sample strings are as follows : 1 - #ff00000 2 -#ff347820 How to validate the above strings to check whether they are valid hex color codes or not . Can anyone help me in sorting out this issue ? Thanks in advance