Search code examples
androidkotlinandroid-xmlandroid-themeandroid-gridlayout

problem with displaying grid layout while app has different theme than system


i have a problem with my elements in Gridlayout which are not loading properly. If my android app has a different theme than the system of my phone, elements are not visible.

I have normal grid layout :

<GridLayout
        android:id="@+id/gridLayoutMainPage"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:alignmentMode="alignMargins"
        android:columnCount="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view5"
        app:layout_constraintVertical_bias="0.5" />

And the function that takes listo of object (that holds the info about each element), and go through and creates a card and adds it into layout :

fun generateCards(cards : List<CardDataHolder>, gridLayout: GridLayout) {
        for (card in cards) {
            val cardview: View = LayoutInflater.from(context)
                .inflate(R.layout.item_main_page_cardview, gridLayout, false)
            val params = GridLayout.LayoutParams()
            params.width = GridLayout.LayoutParams.WRAP_CONTENT;
            params.height = GridLayout.LayoutParams.WRAP_CONTENT;
            params.setMargins(10, 25, 10, 25);
            cardview.layoutParams = params;
            cardview.findViewById<TextView>(R.id.title_card_main_page).text = card.title
            cardview.findViewById<ImageView>(R.id.icon_card_main_page).setImageResource(card.icon)

            cardview.setOnClickListener(View.OnClickListener {
                switchFragment(card.fragment)
                card.fragment.view?.invalidate()
            })

            gridLayout.addView(cardview);
        }
    }

but if app theme is same as phone system theme (light/light or dark/dark) it works correctly, but otherwise it dont display anything. just static content like text views and image views that are defined in xml file. but gridlayout is not filled up with the cards.

Can anybody helps me with that? i tried some after revalidation but i cant figure it out. Thanks

I tried to revalidate the views after the change, becouse i guess that firstly the application launches with the default system theme, and then it swaps the theme. so i tried after swap revalidation, and move the theme swap functionali to the end so it will happened after all elements are initialized but, it didnt work even on the start or end of the code.

Correct state (app and system theme is the same)

bad state (app and system theme are different)


Solution

  • I found out what was happening. When app launched, the system color theme was applied on it. than the values were initialized and view was rendered.But if system color theme was different than app theme, there was a step where app theme was "hard" set to its theme not the systems. and when this step was made Fragments were rendered again but this time values weren't passed into them, becouse this happens only in initialization. so result is :

    Fragment was rendered with correct values, than re-rendered but values were lost by garbage collector and then user didn't see correct view.

    solution: not passing data manualy when declaring the fragment, but make functionality that automaticaly fetches data into fragment when creating so even when i made new instance it fetches correct data.