Example usage for android.view View TEXT_DIRECTION_RTL

List of usage examples for android.view View TEXT_DIRECTION_RTL

Introduction

In this page you can find the example usage for android.view View TEXT_DIRECTION_RTL.

Prototype

int TEXT_DIRECTION_RTL

To view the source code for android.view View TEXT_DIRECTION_RTL.

Click Source Link

Document

Text direction is forced to RTL.

Usage

From source file:Main.java

public static int getTypeOfTextDirection(String text) {
    Matcher matcher = RTL_CHAR.matcher(text);
    if (matcher.find()) {
        return View.TEXT_DIRECTION_RTL;
    } else {//from w  w w  .ja va 2  s . c o  m
        return View.TEXT_DIRECTION_LTR;
    }
}

From source file:com.markupartist.sthlmtraveling.ui.view.TripView.java

public void updateViews() {
    this.setOrientation(VERTICAL);

    float scale = getResources().getDisplayMetrics().density;

    this.setPadding(getResources().getDimensionPixelSize(R.dimen.list_horizontal_padding),
            getResources().getDimensionPixelSize(R.dimen.list_vertical_padding),
            getResources().getDimensionPixelSize(R.dimen.list_horizontal_padding),
            getResources().getDimensionPixelSize(R.dimen.list_vertical_padding));

    LinearLayout timeStartEndLayout = new LinearLayout(getContext());
    TextView timeStartEndText = new TextView(getContext());
    timeStartEndText.setText(DateTimeUtil.routeToTimeDisplay(getContext(), trip));
    timeStartEndText.setTextColor(ContextCompat.getColor(getContext(), R.color.body_text_1));
    timeStartEndText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

    ViewCompat.setPaddingRelative(timeStartEndText, 0, 0, 0, (int) (2 * scale));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (RtlUtils.isRtl(Locale.getDefault())) {
            timeStartEndText.setTextDirection(View.TEXT_DIRECTION_RTL);
        }/*w w  w  . j  a v a2s  .  co m*/
    }

    // Check if we have Realtime for start and or end.
    boolean hasRealtime = false;
    Pair<Date, RealTimeState> transitTime = trip.departsAt(true);
    if (transitTime.second != RealTimeState.NOT_SET) {
        hasRealtime = true;
    } else {
        transitTime = trip.arrivesAt(true);
        if (transitTime.second != RealTimeState.NOT_SET) {
            hasRealtime = true;
        }
    }
    //        if (hasRealtime) {
    //            ImageView liveDrawable = new ImageView(getContext());
    //            liveDrawable.setImageResource(R.drawable.ic_live);
    //            ViewCompat.setPaddingRelative(liveDrawable, 0, (int) (2 * scale), (int) (4 * scale), 0);
    //            timeStartEndLayout.addView(liveDrawable);
    //
    //            AlphaAnimation animation1 = new AlphaAnimation(0.4f, 1.0f);
    //            animation1.setDuration(600);
    //            animation1.setRepeatMode(Animation.REVERSE);
    //            animation1.setRepeatCount(Animation.INFINITE);
    //
    //            liveDrawable.startAnimation(animation1);
    //        }

    timeStartEndLayout.addView(timeStartEndText);

    LinearLayout startAndEndPointLayout = new LinearLayout(getContext());
    TextView startAndEndPoint = new TextView(getContext());
    BidiFormatter bidiFormatter = BidiFormatter.getInstance(RtlUtils.isRtl(Locale.getDefault()));
    startAndEndPoint.setText(String.format("%s  %s", bidiFormatter.unicodeWrap(trip.fromStop().getName()),
            bidiFormatter.unicodeWrap(trip.toStop().getName())));

    startAndEndPoint.setTextColor(getResources().getColor(R.color.body_text_1)); // Dark gray
    startAndEndPoint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (RtlUtils.isRtl(Locale.getDefault())) {
            startAndEndPoint.setTextDirection(View.TEXT_DIRECTION_RTL);
        }
    }
    ViewCompat.setPaddingRelative(startAndEndPoint, 0, (int) (4 * scale), 0, (int) (4 * scale));
    startAndEndPointLayout.addView(startAndEndPoint);

    RelativeLayout timeLayout = new RelativeLayout(getContext());

    LinearLayout routeChanges = new LinearLayout(getContext());
    routeChanges.setGravity(Gravity.CENTER_VERTICAL);

    LinearLayout.LayoutParams changesLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT);
    changesLayoutParams.gravity = Gravity.CENTER_VERTICAL;

    if (trip.hasAlertsOrNotes()) {
        ImageView warning = new ImageView(getContext());
        warning.setImageResource(R.drawable.ic_trip_deviation);
        ViewCompat.setPaddingRelative(warning, 0, (int) (2 * scale), (int) (4 * scale), 0);
        routeChanges.addView(warning);
    }

    int currentTransportCount = 1;
    int transportCount = trip.getLegs().size();
    for (Leg leg : trip.getLegs()) {
        if (!leg.isTransit() && transportCount > 3) {
            if (leg.getDistance() < 150) {
                continue;
            }
        }
        if (currentTransportCount > 1 && transportCount >= currentTransportCount) {
            ImageView separator = new ImageView(getContext());
            separator.setImageResource(R.drawable.transport_separator);
            ViewCompat.setPaddingRelative(separator, 0, 0, (int) (2 * scale), 0);
            separator.setLayoutParams(changesLayoutParams);
            routeChanges.addView(separator);
            if (RtlUtils.isRtl(Locale.getDefault())) {
                ViewCompat.setScaleX(separator, -1f);
            }
        }

        ImageView changeImageView = new ImageView(getContext());
        Drawable transportDrawable = LegUtil.getTransportDrawable(getContext(), leg);
        changeImageView.setImageDrawable(transportDrawable);
        if (RtlUtils.isRtl(Locale.getDefault())) {
            ViewCompat.setScaleX(changeImageView, -1f);
        }
        ViewCompat.setPaddingRelative(changeImageView, 0, 0, 0, 0);
        changeImageView.setLayoutParams(changesLayoutParams);
        routeChanges.addView(changeImageView);

        if (currentTransportCount <= 3) {
            String lineName = leg.getRouteShortName();
            if (!TextUtils.isEmpty(lineName)) {
                TextView lineNumberView = new TextView(getContext());
                lineNumberView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                RoundedBackgroundSpan roundedBackgroundSpan = new RoundedBackgroundSpan(
                        LegUtil.getColor(getContext(), leg), Color.WHITE,
                        ViewHelper.dipsToPix(getContext().getResources(), 4));
                SpannableStringBuilder sb = new SpannableStringBuilder();
                sb.append(lineName);
                sb.append(' ');
                sb.setSpan(roundedBackgroundSpan, 0, lineName.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                lineNumberView.setText(sb);

                ViewCompat.setPaddingRelative(lineNumberView, (int) (5 * scale), (int) (1 * scale),
                        (int) (2 * scale), 0);
                lineNumberView.setLayoutParams(changesLayoutParams);
                routeChanges.addView(lineNumberView);
            }
        }

        currentTransportCount++;
    }

    TextView durationText = new TextView(getContext());
    durationText.setText(DateTimeUtil.formatDetailedDuration(getResources(), trip.getDuration() * 1000));
    durationText.setTextColor(ContextCompat.getColor(getContext(), R.color.body_text_1));
    durationText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    durationText.setTypeface(Typeface.DEFAULT_BOLD);

    timeLayout.addView(routeChanges);
    timeLayout.addView(durationText);

    RelativeLayout.LayoutParams durationTextParams = (RelativeLayout.LayoutParams) durationText
            .getLayoutParams();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        durationTextParams.addRule(RelativeLayout.ALIGN_PARENT_END);
    } else {
        durationTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    }
    durationText.setLayoutParams(durationTextParams);

    View divider = new View(getContext());
    ViewGroup.LayoutParams dividerParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
    divider.setLayoutParams(dividerParams);

    this.addView(timeLayout);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) routeChanges.getLayoutParams();
    params.height = LayoutParams.MATCH_PARENT;
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    routeChanges.setLayoutParams(params);

    this.addView(startAndEndPointLayout);
    this.addView(timeStartEndLayout);

    if (mShowDivider) {
        this.addView(divider);
    }
}

From source file:com.jaredrummler.materialspinner.MaterialSpinner.java

private void init(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialSpinner);
    int defaultColor = getTextColors().getDefaultColor();
    backgroundColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_background_color, Color.WHITE);
    textColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_text_color, defaultColor);
    arrowColor = typedArray.getColor(R.styleable.MaterialSpinner_ms_arrow_tint, textColor);
    hideArrow = typedArray.getBoolean(R.styleable.MaterialSpinner_ms_hide_arrow, false);
    typedArray.recycle();//  ww w .j a  v  a  2s. c  o  m

    setGravity(Gravity.CENTER_VERTICAL | Gravity.START);

    boolean rtl = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Configuration config = getResources().getConfiguration();
        rtl = config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
        if (rtl) {
            setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
            setTextDirection(View.TEXT_DIRECTION_RTL);
        }
    }

    Resources resources = getResources();
    int left, right, bottom, top;
    left = right = bottom = top = resources.getDimensionPixelSize(R.dimen.ms__padding_top);
    if (rtl) {
        right = resources.getDimensionPixelSize(R.dimen.ms__padding_left);
    } else {
        left = resources.getDimensionPixelSize(R.dimen.ms__padding_left);
    }

    setClickable(true);
    setPadding(left, top, right, bottom);
    setBackgroundResource(R.drawable.ms__selector);

    if (!hideArrow) {
        arrowDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ms__arrow));
        DrawableCompat.setTint(arrowDrawable, arrowColor);
        if (rtl) {
            setCompoundDrawablesWithIntrinsicBounds(arrowDrawable, null, null, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null, null, arrowDrawable, null);
        }
    }

    listView = new ListView(context);
    listView.setId(getId());
    listView.setDivider(null);
    listView.setItemsCanFocus(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position >= selectedIndex && position < adapter.getCount()) {
                position++;
            }
            selectedIndex = position;
            nothingSelected = false;
            Object item = adapter.get(position);
            adapter.notifyItemSelected(position);
            setText(item.toString());
            collapse();
            if (onItemSelectedListener != null) {
                //noinspection unchecked
                onItemSelectedListener.onItemSelected(MaterialSpinner.this, position, id, item);
            }
        }
    });

    popupWindow = new PopupWindow(context);
    popupWindow.setContentView(listView);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.setElevation(16);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ms__drawable));
    } else {
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ms__drop_down_shadow));
    }

    if (backgroundColor != Color.WHITE) { // default color is white
        setBackgroundColor(backgroundColor);
    }
    if (textColor != defaultColor) {
        setTextColor(textColor);
    }

    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

        @Override
        public void onDismiss() {
            if (nothingSelected && onNothingSelectedListener != null) {
                onNothingSelectedListener.onNothingSelected(MaterialSpinner.this);
            }
            if (!hideArrow) {
                animateArrow(false);
            }
        }
    });
}

From source file:com.pdftron.pdf.utils.Utils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void showToast(Context context, String string) {
    if (context != null) {
        Toast toast = Toast.makeText(context, string, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        if (Utils.isRtlLayout(context)) {
            toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
        }/* w  w w.  jav  a2 s  .  co  m*/
        toast.show();
        return;
    }
}

From source file:com.pdftron.pdf.utils.Utils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void showToast(Context context, int stringId) {
    if (context != null) {
        Toast toast = Toast.makeText(context, stringId, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        if (Utils.isRtlLayout(context)) {
            toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
        }/* w w  w.j ava  2  s  .  c om*/
        toast.show();
    }
}

From source file:com.pdftron.pdf.utils.Utils.java

public static void showToast(Context context, String string, int duration) {
    if (context != null) {
        Toast toast = Toast.makeText(context, string, duration);
        if (Utils.isRtlLayout(context)) {
            toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
        }/*from   ww  w .  ja  v a2  s . com*/
        toast.show();
    }
}

From source file:com.pdftron.pdf.utils.Utils.java

public static void showToast(Context context, int stringId, int duration) {
    if (context != null) {
        Toast toast = Toast.makeText(context, stringId, duration);
        if (Utils.isRtlLayout(context)) {
            toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
        }/*from  w w w. j a v  a  2  s  . c om*/
        toast.show();
    }
}

From source file:com.pdftron.pdf.utils.Utils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Toast showToast(Context context, Toast toast, String text, int duration) {
    if (toast != null) {
        toast.cancel();/* w w  w.j  av  a 2  s .c  o m*/
    }
    toast = Toast.makeText(context, text, duration);
    if (Utils.isRtlLayout(context)) {
        toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
    }
    toast.show();
    return toast;
}

From source file:com.pdftron.pdf.utils.Utils.java

public static Toast showToast(Context context, Toast toast, int stringId, int duration) {
    if (toast != null) {
        toast.cancel();//from w ww  .j  a v a 2 s  .  c o m
    }
    toast = Toast.makeText(context, stringId, duration);
    if (Utils.isRtlLayout(context)) {
        toast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
    }
    toast.show();
    return toast;
}

From source file:com.pdftron.pdf.utils.Utils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void showCommonToast(@NonNull Context context, String str, int duration) {
    if (sToast == null) {
        sToast = Toast.makeText(context, str, duration);
    } else if (str != sToastStr) {
        sToast.cancel();//from   www.j  a v  a 2s.  c  o  m
        sToast = Toast.makeText(context, str, duration);
    }

    sToastStr = str;
    if (Utils.isRtlLayout(context)) {
        sToast.getView().setTextDirection(View.TEXT_DIRECTION_RTL);
    } else {
        sToast.getView().setTextDirection(View.TEXT_DIRECTION_LTR);
    }
    sToast.show();
}