B4X Store
jdashboardUI / Core & Utilities / ResponsiveUtils
Core & Utilities

ResponsiveUtils

Percentage-based responsive layouts, Form sizing, HiDPI scale and multi-monitor positioning

Search all jdashboardUI docs
Source
jdashboardUI.b4xlib · Public API only
Store language: en

Core & Utilities · jdashboardUI 24 · B4J / JavaFX

ResponsiveUtils is a helper class for building interfaces that adapt to the size of a B4XView, a Form, and the screen currently selected for the user.

On B4J it provides helpers for:

  • percentage-based width and height calculations;
  • centering a Form;
  • sizing a Form as a percentage of the screen;
  • obtaining screen bounds and output scale;
  • multi-monitor positioning;
  • recalculating View layouts during Resize events.

The class does not automatically rearrange your UI. It provides calculations and helpers that your code applies where needed.

Platform

The ResponsiveUtils.bas source contains conditional branches for B4J and B4A.

However, the analyzed jdashboardUI 24 bundle declares:

Supported Platforms=B4j

Therefore the primary documentation for this packaged release is B4J. The B4A branch that exists in the source is documented later as a source-code reference.

Dependencies

ResponsiveUtils uses:

  • B4XView / jXUI;
  • JFX on B4J;
  • JavaObject for JavaFX output-scale access;
  • B4J Screen and Form.

It does not require its own AdditionalJar.

Initialize

Link the helper to a base View:

Private Responsive As ResponsiveUtils

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main")

    Responsive.Initialize(MainForm.RootPane)
    MainForm.Show
End Sub

Signature:

Responsive.Initialize(v As B4XView)

The View is stored internally as mBase.

Use the Root, a main Pane, or another View whose current dimensions should act as 100%.

GetPercentX

Returns a percentage of the current width of the linked View.

Dim W As Double = Responsive.GetPercentX(80)

Formula:

mBase.Width * (Percent / 100)

There is no internal clamp, so values above 100 or below 0 are still calculated mathematically.

GetPercentY

Returns a percentage of the current height of the linked View.

Dim HeaderHeight As Double = Responsive.GetPercentY(12)
Dim ContentHeight As Double = Responsive.GetPercentY(80)

GetBaseView

Returns the original View passed to Initialize.

Dim Base As B4XView = Responsive.GetBaseView

Resize pattern

ResponsiveUtils does not listen for size changes itself. Recalculate your layout from a Resize event.

Private Sub B4XPage_Resize (Width As Int, Height As Int)
    Dim Margin As Double = Responsive.GetPercentX(2)
    Dim HeaderH As Double = Responsive.GetPercentY(12)

    pnlHeader.SetLayoutAnimated(0, _
        Margin, Margin, _
        Responsive.GetPercentX(96), _
        HeaderH)
End Sub

All percentages use the current size of mBase.

Active-screen selection

The public Form methods use the private GetScreenForForm helper.

Screen selection follows this order:

  1. the screen containing the current mouse pointer;
  2. if that fails, the screen containing the horizontal center of an initialized Form with valid coordinates;
  3. fx.PrimaryScreen as fallback.

This makes the helpers useful in multi-monitor environments.

If the mouse is currently on another monitor, that monitor can be selected even if the Form has not yet moved there.

CenterForm

Responsive.CenterForm(MainForm)

The Form is centered using the selected screen's MinX, MinY, width and height.

GetScreenDimensions

Dim Info As Map = Responsive.GetScreenDimensions(MainForm)

Log(Info.Get("Width"))
Log(Info.Get("Height"))
Log(Info.Get("Scale"))

Returned keys:

Key Expected type Meaning
Width Double Screen width
Height Double Screen height
MinX Double Minimum X coordinate
MinY Double Minimum Y coordinate
MaxX Double Maximum X coordinate
MaxY Double Maximum Y coordinate
Scale Double JavaFX output scale

The scale is queried through Screen.getOutputScaleX(). If this is unavailable or fails, the fallback value is 1.0.

HiDPI detection

Dim Info As Map = Responsive.GetScreenDimensions(MainForm)
Dim Scale As Double = Info.Get("Scale")

If Scale > 1 Then
    Log("HiDPI scaling detected: " & Scale)
End If

ResponsiveUtils reports the scale; it does not automatically rescale fonts or Views.

SetFormSizeByPercentage

Responsive.SetFormSizeByPercentage(MainForm, 80, 70)

This sets the Form to 80% of the selected screen width and 70% of its height, then centers it.

The source comments specify values from 0 to 100. The current implementation does not clamp them.

GetFormRelativeWidth

Dim SidebarW As Double = Responsive.GetFormRelativeWidth(MainForm, 20)

Formula:

f.Width * (Percent / 100)

GetFormRelativeHeight

Dim ToolbarH As Double = Responsive.GetFormRelativeHeight(MainForm, 8)

Formula:

f.Height * (Percent / 100)

Complete B4J example

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form

    Private Responsive As ResponsiveUtils

    Private pnlSidebar As B4XView
    Private pnlContent As B4XView
    Private pnlHeader As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main")

    Responsive.Initialize(MainForm.RootPane)
    Responsive.SetFormSizeByPercentage(MainForm, 85, 80)

    MainForm.Show
End Sub

Private Sub MainForm_Resize (Width As Double, Height As Double)
    Dim Margin As Double = Responsive.GetPercentX(2)
    Dim HeaderH As Double = Responsive.GetPercentY(10)
    Dim SidebarW As Double = Responsive.GetPercentX(22)

    pnlHeader.SetLayoutAnimated(0, _
        Margin, Margin, _
        Responsive.GetPercentX(96), _
        HeaderH)

    pnlSidebar.SetLayoutAnimated(0, _
        Margin, _
        HeaderH + Margin * 2, _
        SidebarW, _
        Responsive.GetPercentY(84))

    pnlContent.SetLayoutAnimated(0, _
        SidebarW + Margin * 2, _
        HeaderH + Margin * 2, _
        Responsive.GetPercentX(96) - SidebarW - Margin, _
        Responsive.GetPercentY(84))
End Sub

Multi-monitor coordinates

Dim ScreenInfo As Map = Responsive.GetScreenDimensions(MainForm)

Log("Origin: " & ScreenInfo.Get("MinX") & ", " & ScreenInfo.Get("MinY"))
Log("Size: " & ScreenInfo.Get("Width") & " x " & ScreenInfo.Get("Height"))

MinX and MinY can be negative when a secondary display is positioned to the left of or above the primary display.

B4A branch present in the source

Although the current jdashboardUI 24 bundle is declared B4J-only, ResponsiveUtils.bas includes conditional B4A implementations for:

GetScreenDimensions As Map
GetFormRelativeWidth(Percent As Double) As Double
GetFormRelativeHeight(Percent As Double) As Double

The B4A screen dimensions come from GetDeviceLayoutValues, while relative width and height use 100%x and 100%y.

This source branch is documented for completeness; it does not change the supported-platform declaration of the packaged library.

Complete public API

Common API

Method Return Purpose
Initialize(v As B4XView) - Link the base View
GetPercentX(Percent As Double) Double Percentage of mBase width
GetPercentY(Percent As Double) Double Percentage of mBase height
GetBaseView B4XView Return mBase

B4J API

Method Return Purpose
CenterForm(f As Form) - Center Form on selected screen
GetScreenDimensions(f As Form) Map Screen bounds and scale
SetFormSizeByPercentage(f, WidthPercent, HeightPercent) - Size and center Form
GetFormRelativeWidth(f, Percent) Double Percentage of Form width
GetFormRelativeHeight(f, Percent) Double Percentage of Form height

B4A source API

Method Return Purpose
GetScreenDimensions Map Device dimensions and scale
GetFormRelativeWidth(Percent) Double Percentage of 100%x
GetFormRelativeHeight(Percent) Double Percentage of 100%y

Designer, events and AddToParent

ResponsiveUtils is a utility class:

  • no Designer Properties;
  • no events;
  • no AddToParent;
  • does not create Views;
  • does not automatically modify your layout;
  • requires Initialize with a B4XView.

Best practices

  • Initialize it with the container that really represents your responsive area.
  • Recalculate percentages in Resize events for dynamic layouts.
  • Use SetFormSizeByPercentage for initial Form sizing rather than continuously during resize.
  • Keep screen-size percentages in the intended 0–100 range.
  • Remember that B4J screen detection gives priority to the mouse-pointer monitor.
  • Do not assume MinX and MinY are always zero.
  • Treat Scale as information; ResponsiveUtils does not apply automatic UI scaling.
  • If taskbar/dock usable bounds matter, verify whether full Screen bounds are appropriate for your layout.