Search code examples
androidandroid-layoutthemesandroid-theme

Apply colors from database on Android App


I have this List of themes in a database used by a desktop app.

So let's say I have the Base application theme for my android app:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@android:color/white</item>
</style>

In the desktop App, the themes can be edited and new themes can be added. (So, it would be problematic to hardcode all the existing themes using themes.xml or styles.xml).

  1. I already have a way of getting the colors from the database in a Json format.
  2. I thought I could use data binding. But I couldn't find how to write a code for passing the hex string values.
  3. I wish I could apply the values from the database in the startUp of the app(No need of being exactly 'at runtime').
  4. I saw that I could use the shared Preferences. But I don't know how to apply the hex string from the shared preferences to all app activities.
  5. I wish to apply the 'new colors' to the app in startup and reach all the remaining activities...

The Json with one of the themes look like this:

    [
        {
            "id_temas": 1,
            "tema_nome": "Padrão",
            "bd_barratitulo": "#FF00308B",
            "bd_fundo": "#FFD2E8EC",
            "bd_titulo": "#FFFFFFFF",
            "linear_1a": "#FF002E8A",
            "linear_1b": "#FF0071B7",
            "linear_1c": "#FF002E8A",
            "linear_2a": "#4C8BC0",
            "linear_2b": "#015CA3",
            "linear_2c": "#024795",
            "linear_2d": "#2C5198",
            "linear_3a": "#000098",
            "linear_3b": "#000075",
            "linear_3c": "#000075",
            "linear_3d": "#000082",
            "person": 0
        }
    ]

So, the question is: How can I retrieve the hex strings that I have stored in my database and apply to the android activities? The idea is that when the user logs In, the bars and backgrounds etc, get the colors from one of the database's themes list.

Very Thanks in advance.


Solution

  • If you want to set background color for ex try this:

    currentLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));

    Where #FFFFFF is the string from your json