Search code examples
androidviewandroid-custom-view

How to set an image in the background of custom view


How can one set image from uri to the background of custom view without causing the stackoverflow error. As I couldn't find any appropriate answers, I hope to find it here.


Solution

    1. Get that image via Picasso / Glide libraries as a Bitmap
    2. Convert Bitmap to drawable

      Drawable drawable = new BitmapDrawable(getResources(), bitmap);

    3. Set drawable to your custom view

      yourCustomView.setBackground(drawable);