Bidi

The Bidi class (from java.text) handles bidirectional text layout, which is required when displaying text that mixes left-to-right (LTR) and right-to-left (RTL) scripts (e.g., English + Arabic or Hebrew). It’s primarily used for text rendering engines and internationalized UIs.

Key Features:

  • Supports Unicode Bidirectional Algorithm
  • Determines the visual order of characters based on their directionality
  • Can work with whole text or line-by-line processing
  • Critical for multilingual applications

Commonly Used Methods

Simple Program

import java.text.Bidi;

public class SimpleBidiExample {
    public static void main(String[] args) {
        String mixedText = "English עברית Arabic";

        Bidi bidi = new Bidi(mixedText, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);

        if (bidi.isMixed()) {
            System.out.println("The text contains mixed LTR and RTL directions.");
        } else {
            System.out.println("The text is in a single direction.");
        }
    }
}
/*
The text contains mixed LTR and RTL directions.
*/

Problem Statement

Paani and Mahesh are building a cross-language instant messaging app. Messages can contain mixed English (LTR) and Arabic (RTL). They must display each message correctly based on its text direction. They use the Bidi class to analyze the direction of each message and adjust rendering.

import java.text.Bidi;
import java.util.*;

public class ChatMessageDirectionAnalyzer {
    public static void main(String[] args) {
        List<String> messages = Arrays.asList(
            "Hello Mahesh!",                          // LTR
            "مرحبا LotusJavaPrince",                 // RTL
            "Your meeting is at 3 مساءً",              // Mixed
            "שלום שלום Hello"                         // Mixed (Hebrew + English)
        );

        for (String msg : messages) {
            Bidi bidi = new Bidi(msg, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);

            System.out.println("\nMessage: " + msg);
            System.out.println("Is Mixed: " + bidi.isMixed());
            System.out.println("Is LTR: " + bidi.isLeftToRight());
            System.out.println("Is RTL: " + bidi.isRightToLeft());

            int runCount = bidi.getRunCount();
            System.out.println("Directional Runs: " + runCount);

            for (int i = 0; i < runCount; i++) {
                int start = bidi.getRunStart(i);
                int limit = bidi.getRunLimit(i);
                int level = bidi.getRunLevel(i);
                System.out.printf("Run %d: '%s', Level: %d%n", i, msg.substring(start, limit), level);
            }
        }
    }
}
/*
Message: Hello Mahesh!
Is Mixed: false
Is LTR: true
Is RTL: false
Directional Runs: 1
Run 0: 'Hello Mahesh!', Level: 0

Message: مرحبا LotusJavaPrince
Is Mixed: true
Is LTR: false
Is RTL: true
Directional Runs: 2
Run 0: 'مرحبا ', Level: 1
Run 1: 'LotusJavaPrince', Level: 0

...
*/

The Bidi class is essential when dealing with mixed-directional text, particularly in UI design for global apps:

  • Dealing with bidirectional scripts like Arabic, Hebrew, Farsi, and Urdu
  • Building chat systems, word processors, PDF/text renderers
  • Implementing custom rendering logic in Java-based UI frameworks
Scroll to Top