Description
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.
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