Centering Divs, Items, or Cards in Roku App Development

Amitdogra
2 min readMar 2, 2024

--

In this guide, we’ll explore how to center a div, item, or card in a Roku application using simple and effective techniques.

ITEM is centered in ROKU UI

Code Implementation: Let’s dive into the code snippet below, which demonstrates how to center a target item within a Roku application:

item = m.top.findNode("targetItem")
examplerect = example.boundingRect()
width = 1280
height = 720
centerx = (width - examplerect.width) / 2
centery = (height - examplerect.height) / 2
item.translation = [ centerx, centery ]

Explanation:

  • We first identify the target item within the Roku application using the findNode() method.
  • Next, we determine the bounding rectangle (examplerect) of the item to calculate its dimensions.
  • We define the width and height of the application layout (1280x720 pixels in this example). You can calculate width/height of your roku screen accordingly.
  • Using simple arithmetic, we calculate the center coordinates (centerx and centery) based on the difference between the layout dimensions and the item's dimensions.
  • Finally, we set the translation of the item to the calculated center coordinates, effectively centering it within the application layout.

Note: Experiment with the code snippet and adjust the parameters according to your specific application layout and design requirements.

Happy coding!

Related Topics:

Custom Grid in ROKU- with Code.

Achieve Rounded Corners for item/card/Poster in Roku.

--

--

Amitdogra

Passionate Roku developer with a love for web technologies and backend wizardry. 🚀 ✨ #Android #Streaming #Roku #Web #Nodejs