material-components / material-components/material-components-android

[ShapeableImageView] contentPadding not working when set via Kotlin

Open
#2,063 9 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Feature: Shape
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

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.
![image](https://user-images.githubusercontent.com/40552578/107700234-d037e280-6c7c-11eb-8528-8d3c52a44e0f.png)

**Source code:**

`MainActivity.kt`
```kotlin
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(R.id.kotlin)?.setContentPadding(px, px, px, px)
}
}
```

`activity_main.xml`
```xml

```

`styles.xml`
```xml


<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>

```

**Android API version:** 30

**Material Library version:** 1.3.0

**Device:** Pixel 3 and others

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the difference between app:contentPadding in activity_main.xml and setContentPadding in MainActivity.kt using ShapeableImageView. Read the ShapeableImageView entry point and styles.xml, then verify that programmatic and XML padding produce the same result on the reported Android setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, kotlin
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.