[ShapeableImageView] contentPadding not working when set via Kotlin
See original GitHub issueDescription:
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.
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:
- Created 3 years ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
Ouch, yep I can reproduce this. ShapeableImageView’s
onMeasure
override is causing this ifsetContentPadding
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 apost
: