Android: TextView transparent png as background resource not working

38 views Asked by At

I have a simple TextView inside a CardView (which acts as a button):

<androidx.cardview.widget.CardView
    android:id="@+id/cvTestType"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="10dp">

    <TextView
        android:id="@+id/tvTestType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="15sp" />

</androidx.cardview.widget.CardView>

I'm trying to set a transparent png background to the button as follows:

private fun TextView.setTestTypeTextView(testType: TestTypeDTO) {
    val buttonFontSize = this.context.prefsByScreenDensity()[3]
    this.height = buttonsHeight
    this.text = testType.testTypeName.uppercase(Locale.getDefault())
    this.setTextSize(TypedValue.COMPLEX_UNIT_SP, buttonFontSize.toFloat())
    this.setTextColor(Color.parseColor(props.appearance.colors.appColor))
    
    this.setBackgroundResource(R.drawable.button_act_background)
}

And the problem is, as it can be seen in the screenshot, that using a transparent png as a background is not working, it just displays the button in white with no background at all, it only works when using a non-transparent background.

The PNG is created (and exported) with Gimp and it can be perfectly seen using the Android Studio preview.

Any help?

Screenshot

Edit 1:

Tried different transparent PNGs and noticed that with another PNG I can see the drawing, but the background is still white.

Screenshot 2

1

There are 1 answers

0
Droidman On BEST ANSWER

The root cause is that you are seeing the background of your CardView which seems to be white with your Theme. You can change it using app:cardBackgroundColor="@color/yourColor".