question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ShapeableImageView] contentPadding not working when set via Kotlin

See original GitHub issue

Description: When setting contentPadding on a ShapeableImageView in code (Kotlin or Java), it does not work as expected. Setting app:contentPadding via XML works fine.

Expected behavior: Both elements should match and be like the top element. image

Source code:

MainActivity.kt

package com.example.bug

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import com.google.android.material.imageview.ShapeableImageView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val px = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            8f,
            resources.displayMetrics
        ).toInt()

        findViewById<ShapeableImageView>(R.id.kotlin)?.setContentPadding(px, px, px, px)
    }
}

activity_main.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"
    tools:context=".MainActivity">

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/xml"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/black"
        app:contentPadding="8dp"
        app:layout_constraintBottom_toTopOf="@id/kotlin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/CircleImageView"
        app:srcCompat="@color/teal_700" />

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/kotlin"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/black"
        app:shapeAppearanceOverlay="@style/CircleImageView"
        app:srcCompat="@color/teal_700"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/xml"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CircleImageView">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>
</resources>

Android API version: 30

Material Library version: 1.3.0

Device: Pixel 3 and others

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
J-Swiftcommented, Mar 4, 2022

@drewhamilton no worries, I have no expectations on it. I dont have the bandwidth for it either honestly, I can mostly work around it for now. Appreciate you taking the time to put in the PR and document things here.

1reaction
drewhamiltoncommented, Feb 12, 2021

Ouch, yep I can reproduce this. ShapeableImageView’s onMeasure override is causing this if setContentPadding is called before the view is measured and its layout direction is resolved.

For now you can work around it by only calling setContentPadding after the view is measured and its layout direction is resolved. I’m seeing the expected behavior by wrapping the call in a post:

val imageView = findViewById<ShapeableImageView>(R.id.kotlin)
imageView.post { imageView.setContentPadding(px, px, px, px) }
Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Padding in ImageView is not Working - Stack Overflow
I want to set padding only to the image view. My xml is given below. The padding action is not working. Can anybody...
Read more >
Android Material Lib Shapeableimageview Is Not Showing ...
[ShapeableImageView] contentPadding not working when set via Kotlin Material Library version: Material Android Library version you are using here e.g..
Read more >
ShapeableImageView - Android Developers
Set the padding. This is applied to both the background and the image, and does not affect the content padding differentiating the image...
Read more >
Deep Dive Into ShapeableImageView in Android - ITNEXT
Programmatically with Kotlin. Sometimes the needs to change the shape, attributes, and the corner family dynamically through the code. and ...
Read more >
ShapeableImageView – Material components for android
By using ShapeableImageView, you can change the shape of your image to a circle, diamond, etc. Also, you can set a corner radius...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found