Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.content.res.TypedArray;

import android.os.Build;
import android.support.annotation.ArrayRes;

import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;

public class Main {
    public static int[] getColorArray(@NonNull Context context, @ArrayRes int array) {
        if (array == 0)
            return null;
        TypedArray ta = context.getResources().obtainTypedArray(array);
        int[] colors = new int[ta.length()];
        for (int i = 0; i < ta.length(); i++)
            colors[i] = ta.getColor(i, 0);
        ta.recycle();
        return colors;
    }

    /**
     * Returns a color associated with a particular resource ID
     * <p/>
     * Starting in {@link Build.VERSION_CODES#M}, the returned
     * color will be styled for the specified Context's theme.
     *
     * @param colorId The desired resource identifier, as generated by the aapt
     *                tool. This integer encodes the package, type, and resource
     *                entry. The value 0 is an invalid identifier.
     * @return A single color value in the form 0xAARRGGBB.
     */
    public static int getColor(Context context, @ColorRes int colorId) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            //noinspection deprecation
            return context.getResources().getColor(colorId);
        } else {
            return context.getColor(colorId);
        }
    }
}