Avatar

Avatar is a graphical representation of an object or entity, for example a person or an organisation.

Open in a
new tab
Avatar avatarBasic = new Avatar();

Avatar avatarName = new Avatar(name);

Avatar avatarImage = new Avatar(name);
avatarImage.setImage(pictureUrl);
Note
Real-time collaboration
This component is optimized for use with Collaboration Engine—a simple way to build real-time collaboration into your app—but can also be used standalone as a regular component.

Content

Avatar has three properties: name, abbreviation and image.

Name & Abbreviation

The name is shown on hover in a tooltip. When a name is set, Avatar will auto-generate and display an abbreviation of the specified name. For example, “Allison Torres” becomes “AT”, “John Smith” becomes “JS”, etc.

Open in a
new tab
Avatar avatarName = new Avatar(name);

The abbreviation can also be set manually. Abbreviations should be kept to a maximum of 2–3 characters.

Open in a
new tab
Avatar avatarName = new Avatar("Augusta Ada King");

Avatar avatarAbbr = new Avatar("Augusta Ada King");
avatarAbbr.setAbbreviation("AK");

Image

Avatar can be used to display images, such as user profile pictures or company logos. Abbreviations are not shown when images are used.

Open in a
new tab
Avatar user = new Avatar(name);
user.setImage(pictureUrl);

Avatar company = new Avatar("Company Inc.");
StreamResource imageResource = new StreamResource(
  "company-logo.png",
  () -> getClass().getResourceAsStream("/images/company-logo.png"));
company.setImageResource(imageResource);

Avatar Group

Avatar Group is used to group multiple Avatars together. It can be used, for example, to show that there are multiple users viewing the same page or for listing members of a project.

Open in a
new tab
AvatarGroup avatarGroup = new AvatarGroup();

for (Person person : people) {
  String name = person.getFirstName() + " " + person.getLastName();
  AvatarGroupItem avatar = new AvatarGroupItem(name);
  avatarGroup.add(avatar);
}

Max Number of Items

You can specify the max number of items an Avatar Group should display. Items that overflow are grouped into a single Avatar that displays the overflow count. The name of each hidden item is shown on hover in a tooltip. Clicking the overflow item displays the overflowing avatars and names in a list.

Open in a
new tab
AvatarGroup avatarGroup = new AvatarGroup();
avatarGroup.setMaxItemsVisible(3);

for (Person person : people) {
  String name = person.getFirstName() + " " + person.getLastName();
  AvatarGroup.AvatarGroupItem avatar = new AvatarGroup.AvatarGroupItem(name);
  avatarGroup.add(avatar);
}

Background Color

By default, there are 7 different background colors you can use for Avatar. The background color is set using a color index.

Open in a
new tab
AvatarGroup avatarGroup = new AvatarGroup();

for (Person person : people) {
  String name = person.getFirstName() + " " + person.getLastName();
  AvatarGroupItem avatar = new AvatarGroupItem(name);
  avatar.setColorIndex(colorIndex++);
  avatarGroup.add(avatar);
}

Using different background colors can be useful when you need to be able to distinguish between users, for example during collaborative work.

Internationalisation (i18n)

All of Avatar’s and Avatar Group’s default texts are configurable:

Avatar Texts

PropertyTextDescription

anonymous

"Anonymous"

Avatar’s default name. Shown on hover in a tooltip and announced by screen readers when the Avatar is focused.

Avatar Group Texts

PropertyTextDescription

anonymous

"Anonymous"

Default name for all Avatars in the Avatar Group.

activeUsers.one

"Currently one active user"

Announced by screen readers when there’s exactly one Avatar in an Avatar Group and the Avatar is focused. The name of the Avatar is read aloud first.

activeUsers.many

"Currently {count} active users"

Announced by screen readers when there are multiple Avatars in an Avatar Group and an Avatar is focused. The name of the focused Avatar is read aloud first.

*{count} is the Avatar Group item count.

joined

"{user} joined"

Announced by screen readers when an Avatar is added to the group.

*{user} is the Avatar’s name.

left

"{user} left"

Announced by screen readers when an Avatar is removed from the group.

*{user} is the Avatar’s name.

Open in a
new tab
AvatarGroupI18n i18n = new AvatarGroupI18n();
i18n.setAnonymous("Anonyymi");
i18n.setManyActiveUsers("Yksi käyttäjä aktiivisena");
i18n.setOneActiveUser("{count} käyttäjää aktiivisena");

AvatarGroup avatarGroup = new AvatarGroup();
avatarGroup.setI18n(i18n);

// Add anonymous user
avatarGroup.add(new AvatarGroupItem());

for (Person person : people) {
  String name = person.getFirstName() + " " + person.getLastName();
  AvatarGroupItem avatar = new AvatarGroupItem(name);
  avatarGroup.add(avatar);
}

Size Variants

Avatar has four size variants available:

Open in a
new tab
Avatar xl = new Avatar(name);
xl.addThemeVariants(AvatarVariant.LUMO_XLARGE);

Avatar l = new Avatar(name);
l.addThemeVariants(AvatarVariant.LUMO_LARGE);

Avatar s = new Avatar(name);
s.addThemeVariants(AvatarVariant.LUMO_SMALL);

Avatar xs = new Avatar(name);
xs.addThemeVariants(AvatarVariant.LUMO_XSMALL);
VariantTheme name

Extra large

xlarge

Large

large

Small

small

Extra small

xsmall

Size variants should only be used in special cases.

Use Cases

Avatar can be paired with Menu Bar to create a user account menu.

Open in a
new tab
Avatar avatar = new Avatar(name);
avatar.setImage(pictureUrl);

MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_TERTIARY_INLINE);

MenuItem menuItem = menuBar.addItem(avatar);
SubMenu subMenu = menuItem.getSubMenu();
subMenu.addItem("Profile");
subMenu.addItem("Settings");
subMenu.addItem("Help");
subMenu.addItem("Sign out");