Power Apps – Build a responsive app – Adding Dynamic Containers

Last modified date

In this post we will use dynamic containers to arrange out content so it moves to the planned location at the different screen sizes. We will need to calculate the screen size and then use it to position and size the containers.

This post assumes you’ve done everything in the first 2 posts.

YouTube version

This series is to support my YouTube video.

YouTube image link

The posts for this series are

Calculate Screen Size

In order to make the calculations simple we are going to add a label called AppScreenSize which contains a calculation to classify the screen size to one of small, medium, large and extra large.

For this we are going to use a property of the App object, SizeBreakpoints. It contains a list of numbers for the dividing points of the screen sizes. So under 600 will be small, 600 to 900 will be medium, 900 to 1200 will be large and above 1200 will be extra large.

App SizeBreakpoints

We then add a label, renamed to AppScreenSize and change the text property to the following formula.

AppScreenSize calculation
If(
    App.Width<=First(App.SizeBreakpoints).Value,"Small",           // First Breakpoint
    App.Width<=Last(FirstN(App.SizeBreakpoints,2)).Value,"Medium", // Second Breakpoint
    App.Width<=Last(FirstN(App.SizeBreakpoints,3)).Value,"Large",  // Third Breakpoint
    "Extra Large"
)

Now the label shows “Extra Large”.

Adding Title Bar Container

In my example the title bar is the same for every screen size so is the simplest to position and size. It will start in the top left hand corner so x,y will be 0,0. The height is going to be 80 and the width needs to fit the full screen width. The screen is the parent of the container so we can use Parent.Width.

Within the container I’ve placed a text label that will start in 0,0 and the width and height match are Parent.Width and Parent.Height. I also set the text to be AppScreenSize.Text & ” App”. Finally I formatted the text label.

Title bar dynamic containers

MainTitleContainer

X0
Y0
Height80
WidthParent.Width

First Test

The app is now ready to save and publish. The main test is to check the app resizes correctly and the title bar changes.

first test of dynamic container

Adding Dynamic Containers

Now we are going to add the containers that will move based on the screen size. In total there are 4 containers to add. The left hand side people selector, the right hand side people information which will contain the final 2 containers that sit side by side in large and extra large and above each other on a medium screen.

The next container we add is the container down the left hand side. It needs to fit in below the title bar container and fill to the bottom of the screen. For the small screen size the width will be the whole screen width, for all other sizes it will be 350 wide.

LHS Dynamic Containers

PeopleSelectorContainer

X0
YMainTitleContainer.Height
HeightParent.Height-MainTitleContainer.Height
WidthSwitch(
    AppScreenSize.Text,
    "Small" , Parent.Width ,
    350
)

The next container is the People Info container for the right hand side of the screen. This will fill the remaining part of the screen for all sizes except small when it will be 0 width and not seen.

Note I coloured the containers just so we could see the sizes.

People info dynamic containers

PeopleInfoContainer

XPeopleSelectorContainer.Width
YMainTitleContainer.Height
HeightParent.Height-MainTitleContainer.Height
WidthSwitch(
    AppScreenSize.Text,
    "Small" , 0 ,
    Parent.Width - PeopleSelectorContainer.Width
)

Second Test

With the two dynamic containers added we can publish and test again to make sure the PeopleSelector resizes to full screen for the small size and the PeopleInfoContainer takes up the right hand side when for any size except small.

testing People info dynamic containers

Nested Dynamic Containers

The last two containers will be child containers of the PeopleInfoContainer. For Large and Extra Large screen size they will sit side by side, for Medium screen size they will be one above the other, and for Small it won’t matter because the parent container will be 0 width.

nested People info dynamic containers

PeopleInfo1Container

XPeopleSelectorContainer.Width
YMainTitleContainer.Height
Height400
WidthSwitch(
    AppScreenSize.Text,
    "Medium" , Parent.Width ,
    Parent.Width /2
)

PeopleInfo2Container

XSwitch(
    AppScreenSize.Text,
    "Medium" , Parent.Width ,
    Parent.Width /2
)
YSwitch(
    AppScreenSize.Text,
    "Medium" , PeopleInfo1Container.Height ,
    0
)
HeightSwitch(
    AppScreenSize.Text,
    "Medium" , Parent.Height - PeopleInfo1Container.Height ,
    PeopleInfo1Container.Height
)
WidthSwitch(
    AppScreenSize.Text,
    "Medium" , Parent.Width ,
    Parent.Width /2
)

Final Test

We now have 4 containers that should all be responsive to the screen size, so we save and publish.

final test

Conclusion

Responsive design takes time, lots of time. This is why good web coders are paid the rate they are paid, it is a skilled job. Building an app to be fully responsive will take time. I hope this post gives you enough to get going along with the resources from Microsoft.

Resources

Microsoft have some resources found at

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/create-responsive-layout

Over 20 year experience at being passionate about training, solving problems and loving a new challenge especially in the Microsoft's Power Platform suite.