Skip to content

Instantly share code, notes, and snippets.

@ikhlaqmalik13
Created January 14, 2023 18:11
Show Gist options
  • Save ikhlaqmalik13/34adffdd5dabbe388f25cb6e4846dc21 to your computer and use it in GitHub Desktop.
Save ikhlaqmalik13/34adffdd5dabbe388f25cb6e4846dc21 to your computer and use it in GitHub Desktop.
Adding the layout in Linear Layout dynamically
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".learning.ImagesActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
package com.maple.kashin.learning
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.bumptech.glide.Glide
import com.maple.kashin.databinding.ActivityImagesBinding
import com.maple.kashin.databinding.RowSingleImageBinding
class FollowingModel(val imageUrl: String?, val name: String?, val userName: String?)
class ImagesActivity : AppCompatActivity() {
private lateinit var binding: ActivityImagesBinding
private var images: ArrayList<String> = arrayListOf<String>()
private var followings : ArrayList<FollowingModel> = arrayListOf<FollowingModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityImagesBinding.inflate(layoutInflater)
setContentView(binding.root)
val f1 = FollowingModel(
"https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg",
"Ikhlaq",
"ikhlaqmalik19"
)
followings.add(f1)
setImagesData()
for (imageUrl in images) {
val rowSingleImageBinding: RowSingleImageBinding =
RowSingleImageBinding.inflate(layoutInflater)
Glide.with(this)
.load(imageUrl)
.into(rowSingleImageBinding.ivImage)
binding.llImages.addView(rowSingleImageBinding.root)
}
}
private fun setImagesData() {
images.add("https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg")
images.add("https://i.pinimg.com/564x/44/3e/cb/443ecb7f724bc99256cec196a881cce1.jpg")
images.add("https://i.pinimg.com/736x/1c/3b/41/1c3b41e00fb3efd6df810739af9e4b65.jpg")
images.add("https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg")
images.add("https://i.pinimg.com/564x/44/3e/cb/443ecb7f724bc99256cec196a881cce1.jpg")
images.add("https://i.pinimg.com/736x/1c/3b/41/1c3b41e00fb3efd6df810739af9e4b65.jpg")
images.add("https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg")
images.add("https://i.pinimg.com/564x/44/3e/cb/443ecb7f724bc99256cec196a881cce1.jpg")
images.add("https://i.pinimg.com/736x/1c/3b/41/1c3b41e00fb3efd6df810739af9e4b65.jpg")
}
}
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="310dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:scaleType="centerCrop"
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/dev_amaar" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment