set View Background Drawable - Android Graphics

Android examples for Graphics:Background Drawable

Description

set View Background Drawable

Demo Code


//package com.java2s;
import android.annotation.TargetApi;

import android.content.Context;
import android.content.res.Resources;

import android.os.Build;
import android.view.View;

public class Main {
    /**/*from  w  ww .  j a  v a 2  s .  co  m*/
     * @param ctx
     * @param v
     * @param drawableID
     *       The resource id to drawable
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static void setViewBackgroundDrawable(Context ctx, View v,
            int drawableID) {

        Resources res = ctx.getResources();

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {

            v.setBackgroundDrawable(res.getDrawable(drawableID));

        } else {

            v.setBackground(res.getDrawable(drawableID));
        }
    }
}

Related Tutorials