Search code examples
javaandroidxmlstylesandroid-appcompat

how to set "app:backgroundTint" via style?


I want to replace:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:backgroundTint = "..."
...
    android:orientation="horizontal">

with

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/My.Bg.Snackbar.DayNight"
...
    android:orientation="horizontal">

and

  <style name="My.Bg.Dark" parent="">
    <item name="app:backgroundTint">@color/og_background_dark</item>
  </style>

and

  <declare-styleable name="My">
    <!-- The background color. -->
    <attr name="app:backgroundTint" format="color" />
  </declare-styleable>
</resources>

but I get an error:

error: resource app:attr/backgroundTint not found.

Solution

  • You can use:

      <style name="My.Bg.Dark" parent="">
        <item name="backgroundTint">@color/og_background_dark</item>
      </style>