YogaNode is the main object you will be interfacing with when using Yoga in Java. YogaNode is a thin JNI wrapper around the core Yoga library.

Lifecycle

Create a YogaNode via its default constructor and use reset if you want to pool and re-use nodes. The native memory of a YogaNode will automatically be freed when the node is garbage collected.

1
2
YogaNode();
void reset();

Children

The following methods help manage the children of a node.

1
2
3
4
5
6
int getChildCount();
YogaNodeType getChildAt(int i);
void addChildAt(YogaNodeType child, int i);
YogaNodeType removeChildAt(int i);
YogaNodeType getParent();
int indexOf(YogaNodeType child);

Style getters & setters

The large part of Yoga’s API consists of setters and getters for styles. These all follow the same general structure. Bellow are the function and enums used to control the various styles. For an in depth guide to how each style works see the getting started guide.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
enum YogaDirection {
  INHERIT,
  LTR,
  RTL
}

YogaDirection getStyleDirection();
void setDirection(YogaDirection direction);

enum YogaFlexDirection {
  COLUMN,
  COLUMN_REVERSE,
  ROW,
  ROW_REVERSE
}

YogaFlexDirection getFlexDirection();
void setFlexDirection(YogaFlexDirection flexDirection);

enum YogaJustify {
  FLEX_START,
  CENTER,
  FLEX_END,
  SPACE_BETWEEN,
  SPACE_AROUND
}

YogaJustify getJustifyContent();
void setJustifyContent(YogaJustify justifyContent);

enum YogaAlign {
  AUTO,
  FLEX_START,
  CENTER,
  FLEX_END,
  STRETCH
}

YogaAlign getAlignItems();
void setAlignItems(YogaAlign alignItems);

YogaAlign getAlignSelf();
void setAlignSelf(YogaAlign alignSelf);

YogaAlign getAlignContent();
void setAlignContent(YogaAlign alignContent);

enum YogaPositionType {
  RELATIVE,
  ABSOLUTE
}

YogaPositionType getPositionType();
void setPositionType(YogaPositionType positionType);

enum YogaWrap {
  NO_WRAP,
  WRAP
}

void setWrap(YogaWrap flexWrap);

enum YogaOverflow {
  VISIBLE,
  HIDDEN,
  SCROLL
}

YogaOverflow getOverflow();
void setOverflow(YogaOverflow overflow);

void setFlex(float flex);
float getFlexGrow();
void setFlexGrow(float flexGrow);
float getFlexShrink();
void setFlexShrink(float flexShrink);
float getFlexBasis();
void setFlexBasis(float flexBasis);

enum YogaEdge {
  LEFT,
  TOP,
  RIGHT,
  BOTTOM,
  START,
  END,
  HORIZONTAL,
  VERTICAL,
  ALL
}

float getMargin(YogaEdge edge);
void setMargin(YogaEdge edge, float margin);

float getPadding(YogaEdge edge);
void setPadding(YogaEdge edge, float padding);

float getBorder(YogaEdge edge);
void setBorder(YogaEdge edge, float border);

float getPosition(YogaEdge edge);
void setPosition(YogaEdge edge, float position);

float getWidth();
void setWidth(float width);

float getHeight();
void setHeight(float height);

float getMaxWidth();
void setMaxWidth(float maxWidth);

float getMinWidth();
void setMinWidth(float minWidth);

float getMaxHeight();
void setMaxHeight(float maxHeight);

float getMinHeight();
void setMinHeight(float minHeight);

float getAspectRatio();
void setAspectRatio(float aspectRatio);

Layout results

Once you have set up a tree of nodes with styles you will want to get the result of a layout calculation. Call calculateLayout() perform layout calculation. Once this function returns the results of the layout calculation is stored on each node. Traverse the tree and retrieve the values from each node.

1
2
3
4
5
6
void calculateLayout();
float getLayoutX();
float getLayoutY();
float getLayoutWidth();
float getLayoutHeight();
YogaDirection getLayoutDirection();

Custom measurements

Certain nodes need to ability to measure themselves, the most common example is nodes which represent text. Text has an intrinsic size and requires measuring itself to determine that size. This is not something Yoga can do as it requires relying on the host system’s text rendering engine.

  • Call dirty() if a node with a custom text measurement function needs to be re-measured during the next layout pass.

A measure function can only be attached to a leaf node in the hierarchy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
enum YogaMeasureMode {
  UNDEFINED,
  EXACTLY,
  AT_MOST
}

interface YogaMeasureFunction {
  long measure(
      YogaNode node,
      float width,
      YogaMeasureMode widthMode,
      float height,
      YogaMeasureMode heightMode);
}

class YogaMeasureOutput {
  static long make(int width, int height);
}

void setMeasureFunction(YogaMeasureFunction measureFunction);
boolean isMeasureDefined();

boolean isDirty();
void dirty();

Data

Data is important when integrating Yoga into another layout system. Data allows you to associate another object with a YogaNode. This data can then be retrieved from a YogaNode when for example its measure function is called. This is what enables Yoga to rely on the Android system implementations of text measurement in React Native.

1
2
void setData(Object data);
Object getData();

Logging

Yoga will by default log to stdout and stderr (or logcat on Android). You may however customize this to instead log to your own logger.

1
2
3
4
5
6
7
8
9
10
11
12
13
enum YogaLogLevel {
  ERROR,
  WARN,
  INFO,
  DEBUG,
  VERBOSE
}

interface YogaLogger {
  void log(YogaLogLevel level, String message);
}

void setLogger(YogaLogger logger);

Experiments

Yoga has the concept of experiments. An experiment is a feature which is not yet stable. To enable a feature use the following functions. Once a feature has been tested and is ready to be released as a stable API we will remove its feature flag.

1
2
3
4
5
6
7
8
9
enum YogaExperimentalFeature {
  // Current experiments
}

static void setExperimentalFeatureEnabled(
  YogaExperimentalFeature feature, 
  boolean enabled);
static boolean isExperimentalFeatureEnabled(
  YogaExperimentalFeature feature);