Sabuj Ahmed
Sabuj Ahmed

Reputation: 3

ImageView fit width 100% without stretching the image android kotlin

I am getting an image from the gallery and when I add an image to the layout if the image size is less than the width then the image doesn't fill the width 100%. How do I do it?

<androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/ig_gallery"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:layout_marginVertical="20dp"
        android:src="@drawable/mypic"
        app:layout_constraintBottom_toTopOf="@+id/button_pick"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

class MainActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.buttonPick.setOnClickListener {
            pickImage()
        }
    }

    private fun pickImage() {
        ImagePicker.with(this)
            .galleryOnly()
            .compress(1024)         //Final image size will be less than 1 MB(Optional)
            .maxResultSize(
                650,
                1200
            )  //Final image resolution will be less than 1080 x 1080(Optional)
            .createIntent { intent ->
                startForProfileImageResult.launch(intent)
            }
    }


    private val startForProfileImageResult =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            val resultCode = result.resultCode
            val data = result.data

            when (resultCode) {
                Activity.RESULT_OK -> {
                    //Image Uri will not be null for RESULT_OK
                    val fileUri = data?.data!!
                    binding.igGallery.setImageURI(fileUri)

                }
                ImagePicker.RESULT_ERROR -> {
                    Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
                }
                else -> {
                    Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
                }
            }
        }
}

enter image description here

**I am getting an image from the gallery and when I add an image to the layout if the image size is less than the width then the image doesn't fill the width 100%. How do I do it?

I try it lot but don't get any result**

Upvotes: 0

Views: 440

Answers (1)

Ashfaque
Ashfaque

Reputation: 332

Try This code

     <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/ig_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/mypic"
        app:layout_constraintBottom_toTopOf="@+id/button_pick"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Upvotes: 0

Related Questions