get View Background Color - Android User Interface

Android examples for User Interface:View Background

Description

get View Background Color

Demo Code


//package com.java2s;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class Main {
    public static int getBackgroundColor(View view) {
        Drawable background = view.getBackground();
        if (background == null) {
            throw new NullPointerException("background is null");
        }/*from w w w .jav a2s.c om*/
        if (background instanceof ColorDrawable) {
            final int backgroundColor = ((ColorDrawable) background)
                    .getColor();
            return backgroundColor;
        } else {
            throw new RuntimeException("background isn't ColorDrawable");
        }
    }
}

Related Tutorials