Changing the theme at runtime depending on screen width

I need to change the theme at runtime, depending on screen width and resolution.
I implemented the solution in the UI init method,using a setTheme() method, however this solution produce an delay effect when the theme is changed.
I mean I see the page (the first page) with the default theme and then it changes to the correct theme.
I tried to provide an UIProvider in which I redefine the getTheme() method in order to provide the correct value but I’m not able the get the screen width in this context.
Could someone provide a solution?