skip to Main Content

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**

2

Answers


  1. 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" />
    
    Login or Signup to reply.
  2. In your code, you can use the android:scaleType attribute to control how the image is displayed within the ImageView. To make the image fill the width of the layout, you can use the scaleType value of fitXY. Here’s the code:

    <androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/ig_gallery"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:scaleType="fitXY"
    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" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search