I tried using the library "Picasso" but it needs a pre-created ImageView, and I am trying to put it in a new ImageView but when I put the id that the ImageView will have it gives me an error.
I am using this code to create the ImageView:
val photo = ImageView(applicationContext)
photo.id = variable1
Scroll.addView(photo)
And I am using this code to put the image:
Picasso.get().load("$url").into(variable1)
And this does not work, so I thought it will be a good idea to put the photo in a pre-created ImageView and then getting the image reference of that ImageView and giving it to the new ImageView as:
var photoResource = R.id.borramePerro.getResource()
photo.setImageResource(photoResource)
But it doesn't work. Any ideas?
class InicioSesionGoogle: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.google)}
var urlImagen : String
var variable1 = 1
fun showPhoto(url: String){
val photo = ImageView(applicationContext)
Picasso.get().load("$url").into(variable1)
photo.id = variable1
Scroll.addView(photo)
println("Photo set :D $url with the id of: $variable1")
variable1++
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<ScrollView
android:id="@+id/ScrollVieww"
android:layout_width="310dp"
android:layout_height="204dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/Scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
<ImageView
android:id="@+id/borramePerro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
It's preferable to use View.generateViewId()
to generate id for your views, small numbers might already be occupied by R.
photo.id = View.generateViewId()
You don't need id at all to use Picasso though, just do:
val photo = ImageView(context)
Picasso.get().load(url).into(photo)
Since you are using Kotlin, you might want to take a look in Coil. It's more Kotlin-friendly than Picasso. Also, notice that "$url"
is useless and probably extra work for the CPU (insignificant of course).