com.sbgapps.simplenumberpicker.utils.ThemeUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.sbgapps.simplenumberpicker.utils.ThemeUtil.java

Source

/*
 * Copyright 2017 Stphane Baiget
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sbgapps.simplenumberpicker.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.TypedValue;

/**
 * Created by sbaiget on 08/03/2017.
 */

public class ThemeUtil {

    @ColorInt
    public static int getThemePrimaryColor(Context context) {
        int colorAttr;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            colorAttr = android.R.attr.colorPrimary;
        } else {
            colorAttr = context.getResources().getIdentifier("colorPrimary", "attr", context.getPackageName());
        }
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(colorAttr, outValue, true);
        return outValue.data;
    }

    @ColorInt
    public static int getThemeAccentColor(Context context) {
        int colorAttr;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            colorAttr = android.R.attr.colorAccent;
        } else {
            colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
        }
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute(colorAttr, outValue, true);
        return outValue.data;
    }

    public static StateListDrawable makeSelector(Context context, int drawableResId, int color) {
        StateListDrawable res = new StateListDrawable();
        res.setExitFadeDuration(50);
        Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
        DrawableCompat.setTint(drawable, color);
        res.addState(new int[] { android.R.attr.state_enabled }, drawable);
        drawable = ContextCompat.getDrawable(context, drawableResId);
        DrawableCompat.setTint(drawable, color & 0x40FFFFFF);
        res.addState(new int[] { -android.R.attr.state_enabled }, drawable);
        return res;
    }
}