Example usage for android.text TextDirectionHeuristics RTL

List of usage examples for android.text TextDirectionHeuristics RTL

Introduction

In this page you can find the example usage for android.text TextDirectionHeuristics RTL.

Prototype

TextDirectionHeuristic RTL

To view the source code for android.text TextDirectionHeuristics RTL.

Click Source Link

Document

Always decides that the direction is right to left.

Usage

From source file:android.text.BidiFormatter.java

/**
 * Formats a string of given directionality for use in plain-text output of the context
 * directionality, so an opposite-directionality string is neither garbled nor garbles its
 * surroundings. This makes use of Unicode bidi formatting characters.
 * <p>//  w  w w.ja  v  a 2s  . c o  m
 * The algorithm: In case the given directionality doesn't match the context directionality, wraps
 * the string with Unicode bidi formatting characters: RLE+{@code str}+PDF for RTL text, or
 * LRE+{@code str}+PDF for LTR text.
 * <p>
 * If {@code isolate}, directionally isolates the string so that it does not garble its
 * surroundings. Currently, this is done by "resetting" the directionality after the string by
 * appending a trailing Unicode bidi mark matching the context directionality (LRM or RLM) when
 * either the overall directionality or the exit directionality of the string is opposite to that
 * of the context. If the formatter was built using {@link Builder#stereoReset(boolean)} and
 * passing "true" as an argument, also prepends a Unicode bidi mark matching the context
 * directionality when either the overall directionality or the entry directionality of the
 * string is opposite to that of the context. Note that as opposed to the overall
 * directionality, the entry and exit directionalities are determined from the string itself.
 * <p>
 * Does *not* do HTML-escaping.
 *
 * @param str The input string.
 * @param heuristic The algorithm to be used to estimate the string's overall direction.
 *        See {@link TextDirectionHeuristics} for pre-defined heuristics.
 * @param isolate Whether to directionally isolate the string to prevent it from garbling the
 *     content around it
 * @return Input string after applying the above processing.
 */
public String unicodeWrap(String str, TextDirectionHeuristic heuristic, boolean isolate) {
    final boolean isRtl = heuristic.isRtl(str, 0, str.length());
    StringBuilder result = new StringBuilder();
    if (getStereoReset() && isolate) {
        result.append(markBefore(str, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR));
    }
    if (isRtl != mIsRtlContext) {
        result.append(isRtl ? RLE : LRE);
        result.append(str);
        result.append(PDF);
    } else {
        result.append(str);
    }
    if (isolate) {
        result.append(markAfter(str, isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR));
    }
    return result.toString();
}