Blog

How to implement responsive web design in Java

By  
Matti Tahvonen
Matti Tahvonen
·
On Jul 18, 2024 2:01:57 PM
·

Responsive design aims to improve the usability of your app on different devices and varying window sizes. Many consider responsive web design to be a task dedicated to CSS magicians. With Vaadin, the same end result can also be accomplished in pure Java. In fact, you can achieve much more than what is possible with plain CSS.

In the most trivial form, you can request the true size of the browser window via the Page object and make design decisions based on it.

UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {
    if (details.getBodyClientWidth() < 800) {
        add(new VerticalLayout(cards));
    } else {
        add(new HorizontalLayout(cards));
    }
});

But there are a lot of other tricks you can do. I collected five tips from my own toolbox into an example application, which is essentially a full version of this blog post. 😀

Continue reading for more tips in the example app.

New to the Vaadin platform? Craft stunning full-stack web apps at lightning speed, from hobby projects to enterprise scale. Take it for a spin at start.vaadin.com.

Matti Tahvonen
Matti Tahvonen
Matti Tahvonen has a long history in Vaadin R&D: developing the core framework from the dark ages of pure JS client side to the GWT era and creating number of official and unofficial Vaadin add-ons. His current responsibility is to keep you up to date with latest and greatest Vaadin related technologies. You can follow him on Twitter – @MattiTahvonen
Other posts by Matti Tahvonen