https://kotlinlang.org logo
#android
Title
# android
k

Kulwinder Singh

08/28/2019, 11:34 AM
Hi, i have created this custom view and inside it i'm loading image using picasso like below, this custom view contains many other view with ImageView that's why it is extending
ConstraintLayout
-:
Copy code
class CustomView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs) {
    init {
        var a: TypedArray? = null
	    ......
	    val backgroundImage = a.getResourceId(R.styleable.CustomView_backgroundImage, -0)// image will be from assets
        inflate(context, R.layout.custom_view, this)
        if (backgroundImage != -0) Picasso.get().load(backgroundImage).into(img_background)
            .....
    }
}
with this code app compiles and on device it loads image. but problem is that it don't show image preview on android studio and android studio unable to render layout. any solution to this ?
w

wasyl

08/28/2019, 11:36 AM
You can use
View#isInEditMode
to specify logic for the preview. Since in preview Picasso doesn’t work, it should be fine if you load the image directly then
👍 1
k

Kulwinder Singh

08/28/2019, 11:39 AM
looks
View#isInEditMode
will work, thanks a lot @wasyl, also is my approach loading image inside custom view's constructor using Picasso good ? or there any bad point in this ?
w

wasyl

08/28/2019, 11:40 AM
You are tying your view to specific image loader library, but that’s a general thing and most people don’t care about abstracting that anyway. And afaik it’s annoying to inject stuff to the view anyway
Either way as long as you use any library, you will need
isInEditMode
for such subclass, I think
k

Kulwinder Singh

08/28/2019, 11:43 AM
i have checked
isInEditMode
worked 👍
👍 1
Also i'm only going to load image from
res/drawable/*
folder in this view, don't know if it is efficient using picasso ? can you please let me know about it ? because upto now i was using this view without picasso and don't got any Memory exception
w

wasyl

08/28/2019, 12:33 PM
I think it’s still good to use library, otherwise the loading takes place on the main thread
At least I came to this conclusion last time I was researching this, so if someone is more informed I’d gladly hear more 🙂
k

Kulwinder Singh

08/28/2019, 1:01 PM
It is good i also think, but i had faced another problem when loading image first time it takes some time(delay in loading image) and i am using image as background for my whole layout and it does not look good when background image loaded after some time.
14 Views