Set imageview background using drawable based on API 7 / 8


Here is snippet to set background in Imageview that support API 7 / 8 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
     * Set background image
     *
     * @param imagePath
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
    @SuppressWarnings("deprecation")
    private void setBackgroundImage(final String imagePath) {
        Bitmap bm = openAndResize();
        Drawable drawable = new BitmapDrawable(getApplication().getResources(), bm);
       
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            previewImageView.setBackgroundDrawable(drawable);
        } else {
            previewImageView.setBackground(drawable);
        }
       
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.