I tried to summarize Android development, RGB, hexadecimal number, color implementation from resource files

About Color

I was addicted to it when I was playing with UI design in Android development, so I tried to organize it. You can specify the color from hexadecimal or from the resource file. There is no such thing as a resource file on iOS, so it took me a while to understand the concept. The tricky thing for me was the concept of a Resource file.

Settings from color resources (I made it the top because I am most addicted to it)

A method that can be used if the key is set in color.xml of the Res (Resource) folder. A common method in practice.

Resource.java


Resources res = getResources();
int chocolate_color = res.getColor(R.color.chocolate);

TextView text = new TextView(this);
text.setBackgroundColor(chocolate_color);

color.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="chocolate">#d2691e</color>
</resources>

#Color setting in RGB format

Rgb.java


TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("#FF00C0"));

// "#FF_00_C0"
// RGB

The parseColor method is public static int parseColor(String colorString) So the argument is of type String and the return value is of type ʻint. Color seems to be ʻint color.

Color setting in ARGB format

Argb.java


TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("#FF0F00C0"));

// "#FF_0F_00_C0"
// ARGB

Color setting in keyword format

Keywords that can be used are the keywords below

red blue green black white gray cyan magenta yellow lightgraydarkgray

Word.java



TextView tv = new TextView(this);
tv.setTextColor(Color.parseColor("magenta"));

The default constant from the Color class?

--Color.BLACK: Black --Color.BLUE: Blue --Color.CYAN: Cyan --Color.DKGRAY: Dark gray --Color.GRAY: Gray --Color.GREEN: Green --Color.LTGRAY: Light gray --Color.MAGENTA: Magenta --Color.RED: Red --Color.TRANSPARENT: Transparent --Color.WHITE: White --Color.YELLOW: Yellow

How to convert from hexadecimal number to int and set color (remember)

Intcolor.java


String white = "#ffffff";
int whiteInt = Color.parseColor(white);

TextView text = new TextView(this);
text.setBackgroundColor(whiteInt);

How to convert between hexadecimal strings and bytes

hex.java


public static String bin2hex(byte[] data) {
    StringBuffer sb = new StringBuffer();
    for (byte b : data) {
        String s = Integer.toHexString(0xff & b);
        if (s.length() == 1) {
            sb.append("0");
        }
        sb.append(s);
    }
    return sb.toString();
}

public static byte[] hex2bin(String hex) {
    byte[] bytes = new byte[hex.length() / 2];
    for (int index = 0; index < bytes.length; index++) {
        bytes[index] = (byte) Integer.parseInt(hex.substring(index * 2, (index + 1) * 2), 16);
    }
    return bytes;
}

I think that if you hold down this much, you will be able to perform color conversion immediately after seeing this page.

Recommended Posts

I tried to summarize Android development, RGB, hexadecimal number, color implementation from resource files
Android development, brightness adjustment in HSV from RGB color,
I tried to summarize personally useful apps and development tools (development tools)
I tried to summarize personally useful apps and development tools (Apps)
I tried to summarize iOS 14 support
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the methods used
I tried to summarize the Stream API
Convert from color temperature to RGB (sRGB)
What is Docker? I tried to summarize
I tried to summarize about JVM / garbage collection
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried using a database connection in Android development
I tried to summarize the state transition of docker
I tried to summarize various link_to used this time
App development beginners tried to make an Android calculator app
I tried to chew C # (reading and writing files)