Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : set background of layout using image path

I want to put an image as background of a Layout.

First I am creating a drawable : Drawable d = Drawable.createFromPath("pathToImageFile");

In API level 8 layout.setBackground( d ) is not supported AND layout.setBackgroundDrawable( d ) is deprecated so I need to use

layout.setBackgroundResource(resourceID)

how can I get resourceID of a dynamically generated drawable.I am using this method :

Drawable d = Drawable.createFromPath("pathToImageFile");

to create a drawable.

like image 721
ZZeyaNN Avatar asked Dec 18 '13 08:12

ZZeyaNN


1 Answers

Hi use the following method

public void setBackgroundDrawable (Drawable background) 

by calling

imageView.setBackgroundDrawable(drawable);

Added in API level 1

EDIT: try this method

@SuppressWarnings("deprecation")
    private void setRes(ImageView iv,Drawable drawable){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            iv.setBackground(drawable);
        else
            iv.setBackgroundDrawable(drawable);
    }
like image 184
dinesh sharma Avatar answered Oct 30 '22 21:10

dinesh sharma