0

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)

6
  • Can you show images of the result in normal state and issued state? Commented Dec 27, 2023 at 10:42
  • @TamHuynh i added pictures, is seems to me like the elements in grid layout are not generating, or they were deleted or replaced somehow by changing the theme. Commented Dec 27, 2023 at 10:54
  • Did you try using the Layout Inspector tool to check the layout if any Views are there but invisible? And another thing, you change the phone's theme and come back to the app (stay in the background) then this happens, or do you completely close the app and start again and see the issue? Commented Dec 27, 2023 at 11:14
  • @TamHuynh as you mentioned i tried to use layout inspector but the grid layout is empty. And if I close the app completely, and the app is set to dark mode, and after complete closing i change my system theme to light, then open app (system - light, app - dark) the bad state appears. The same is when i swap the themes (complete close app with light mode, switch system to dark, and open app). And if themes are the same and i open the app, correct state appears and everything is okay, until i change the system theme. Commented Dec 27, 2023 at 11:40
  • If I change the system theme while app running elements in grid layout disapears. until i again enter the fragment. Commented Dec 27, 2023 at 11:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you found the issue. You didn't post the code where you created the fragment nor passed the parameters so it is hard for anyone else to pinpoint the issue correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.