Gets the view background color - Android Graphics

Android examples for Graphics:Background Color

Description

Gets the view background color

Demo Code


//package com.java2s;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.View;
import java.lang.reflect.Field;

public class Main {
    /**/* ww  w  .  j av a2s.com*/
     * Gets the view background color
     * @param textView
     * @return
     */
    public static int getBackgroundColor(View textView) {
        ColorDrawable drawable = (ColorDrawable) textView.getBackground();
        if (Build.VERSION.SDK_INT >= 11) {
            return drawable.getColor();
        }
        try {
            Field field = drawable.getClass().getDeclaredField("mState");
            field.setAccessible(true);
            Object object = field.get(drawable);
            field = object.getClass().getDeclaredField("mUseColor");
            field.setAccessible(true);
            return field.getInt(object);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;
    }
}

Related Tutorials