Set a drawable as background programmatically

EDIT: Sorry i realize from your comment my question was not clear enough. I will post a new one. Sorry for this and thanks for your answers

I am populating a listview from a Json file.
With my listadapter, I can easily assign appropriate json data to each row of my list. That works well for text, for example:

  • Android: Retrieving shared preferences of other application
  • Assigning Fragments to tabs in the ActionBar with different orientations
  • Variable is accessed within inner class. Needs to be declared final
  • Android multidex, UnsatisfiedLinkError - couldn't find .so file
  • Android: ToggleButton Listener
  • Full text search example in Android
  • TextView tv = (TextView)ll.findViewById(;   

    With the above code, every row will be correctly populated by the good json data.

    However, I don’t manage to do the same thing for an image. I have tried to set the right image from my json data using this:

    ImageView iv = (ImageView)ll.findViewById(;           

    I guess I am doing something wrong with the type of my parameters: “setBackgroundDrawable” requires a drawable parameter.
    “getDrawable” requires an int.
    I have set the type of my field img to int, but that doesn’t work.

    Any idea why?

    My list adapter:

    public class adapter extends ArrayAdapter<ListItems> {
    int resource;
    String response;
    Context context;
    //Initialize adapter
    public ListItemsAdapter(Context context, int resource, List<ListItems> items) {
        super(context, resource, items);
    public View getView(int position, View convertView, ViewGroup parent)
        //Get the current object
        ListItems i = getItem(position);
        //Inflate the view
            ll = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater li;
            li = (LayoutInflater)getContext().getSystemService(inflater);
            li.inflate(resource, ll, true);
            ll = (LinearLayout) convertView;
        //For the message
        TextView tv = (TextView)ll.findViewById(;
    // For the Img
        ImageView iv = (ImageView)ll.findViewById(;
        return ll;

    my item class:

        public class ListItems{
    int id;
    int img;    
    String desc;}

    And a sample of my json file:


    Related posts:

    How to send and receive SMS from android app?
    Android support v23.1.0 update breaks NavigationView get/find header
    maxWidth doesn&#039;t work with fill_parent
    Get color-int from color resource
    Android ScrollView adds extra padding on top and bottom of child imageview
    ndk build and .so file creation android
  • Flurry vs localytics?
  • How to make a splash screen (screen visible when app starts)?
  • How to get accent color programmatically?
  • Android SQLite database: slow insertion
  • Long press definition at XML layout, like android:onClick does
  • When does WeakReference#get() start returning null?
  • 3 Solutions collect form web for “Set a drawable as background programmatically”

    Try this




    Now as getDrawable and setBackgroundDrawable both are depricated you should set drawable as Background like this :

    view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 

    and if you are targating minSdk below 16 then make a check like this :

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable));
        } else {
            view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));

    Here the new Method


    don’t use this it’s an old method

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.