Example usage for android.text Layout getLineStart

List of usage examples for android.text Layout getLineStart

Introduction

In this page you can find the example usage for android.text Layout getLineStart.

Prototype

public abstract int getLineStart(int line);

Source Link

Document

Return the text offset of the beginning of the specified line ( 0…getLineCount()).

Usage

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

/**
 * @param editable - edit text//from   w w w.  j  a v a2s . c  om
 * @param line   - current line
 * @param col    - column index at current line
 * @return the index at (line:col)
 */
public static int getIndexFromLineCol(Layout editable, int line, int col) {
    int index = editable.getLineStart(line);
    index += col;
    return Math.min(index, editable.getText().length());
}

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

public static int getStartIndexAtLine(EditText editable, int line) {
    Layout layout = editable.getLayout();
    if (layout != null) {
        return layout.getLineStart(line);
    }//w ww . ja  v  a  2s  . com
    return 0;
}

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

/**
 * Gets the lineInfo from the index of the letter in the text
 *///from w w  w .  j  av a  2s .c  o  m
public static int getLineFromIndex(int index, int lineCount, Layout layout) {
    int line;
    int currentIndex = 0;

    for (line = 0; line < lineCount; line++) {
        currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
        if (currentIndex > index) {
            break;
        }
    }
    return line;
}

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

/**
 * Gets the line from the index of the letter in the text
 * <p>//www  . java 2  s .  c  om
 * 1  2  3  |
 * ^  ^  ^  ^
 * 0  1  2  cursor at 4, return (line;col) = (0;4), line start at 0, column start at 0
 */
@NonNull
public static Pair<Integer, Integer> getLineColFromIndex(int cursorIndex, int length, int lineCount,
        Layout layout) {
    int line;
    int currentIndex = 0, oldIndex = 0;

    line = 0;
    while (line < lineCount) {
        oldIndex = currentIndex;
        currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
        if (currentIndex > cursorIndex) {
            break;
        }
        if (line < lineCount - 1) {
            line++;
        } else {
            break;
        }
    }
    Pair<Integer, Integer> result = new Pair<>(line, cursorIndex - oldIndex);
    DLog.d(TAG, "getLineColFromIndex() returned: " + result);
    return result;

}

From source file:com.amazon.android.ui.widget.EllipsizedTextView.java

/**
 * Sets ellipsis properties.//from   w w w  .  ja  v  a2  s  .  c  o  m
 *
 * @param widthMeasureSpec  Ellipsis width.
 * @param heightMeasureSpec Ellipsis height.
 * @param layout            Layout for ellipsis.
 * @param lastLine          Last line length for ellipsis.
 * @param maxLines          Max lines in ellipsis.
 */
private void setEllipsis(int widthMeasureSpec, int heightMeasureSpec, Layout layout, int lastLine,
        int maxLines) {

    mIsEllipsized = true;
    setFocusable(true);
    setClickable(true);
    final SpannableString ss = new SpannableString(mCharSequence);
    String visibleText = mCharSequence.toString();

    mEllipsisImage = new StateImageSpan(getContext(), mGuillemetDrawableId, ImageSpan.ALIGN_BASELINE);

    final SpannableStringBuilder spannedText = new SpannableStringBuilder();
    int ellipsisIndex = layout.getLineStart(Math.min(lastLine + 1, maxLines));

    // Keep chopping words off until the ellipsis is on a visible line or there is only one
    // line left.
    do {
        // Only truncate the last line for long description.
        if (lastLine >= maxLines) {
            // Getting the first word break index before the last index of maxline.
            int safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getWordInstance());
            final int maxLineStart = layout.getLineStart(maxLines - 1);

            // If this check pass, it means we just truncated a word that is longer than a line.
            if (safeBreakIndex < maxLineStart) {
                // Need to check character by character and break in the middle now. Checking
                // word by word should cover most cases, only do this if a word is longer than
                // line width.
                safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getCharacterInstance());
            }
            ellipsisIndex = safeBreakIndex;
        }

        visibleText = visibleText.substring(0, ellipsisIndex);
        final CharSequence charOutput = ss.subSequence(0, ellipsisIndex);

        // Re-add ellipsis and convert to image
        spannedText.replace(0, spannedText.length(), charOutput);
        spannedText.append(ELLIPSIS);

        spannedText.setSpan(mEllipsisImage, ellipsisIndex, ellipsisIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        // Reset text and re-measure.
        super.setText(spannedText, BufferType.SPANNABLE);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    } while (getLineCount() > getMaxLines() && getLineCount() > 1);
    requestFocus();
}

From source file:uk.ac.horizon.artcodes.activity.ExperienceActivity.java

@Override
public void loaded(Experience experience) {
    super.loaded(experience);
    GoogleAnalytics.trackScreen("View Experience", experience.getId());
    binding.setExperience(experience);/*from w w  w  . ja v  a2  s  .  com*/

    binding.experienceDescription.getViewTreeObserver()
            .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (binding.experienceDescription.getLineCount() > 1) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            binding.experienceDescription.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        }
                        final Layout layout = binding.experienceDescription.getLayout();
                        if (layout != null) {
                            final int lines = layout.getLineCount();
                            int ellipsisCount = 0;
                            for (int index = 0; index < lines; index++) {
                                ellipsisCount += layout.getEllipsisCount(index);
                            }

                            Log.i("Ellipsis", "Lines = " + lines + ", ellipsis = " + ellipsisCount);
                            if (ellipsisCount == 0) {
                                binding.experienceDescriptionMore.setVisibility(View.GONE);
                            } else {
                                final int lineChars = layout.getLineStart(1);
                                if (ellipsisCount < (lineChars * 2)) {
                                    binding.experienceDescription.setMaxLines(Integer.MAX_VALUE);
                                    binding.experienceDescriptionMore.setVisibility(View.GONE);
                                } else {
                                    binding.experienceDescriptionMore.setVisibility(View.VISIBLE);
                                }
                            }
                        }
                    } else {
                        binding.experienceDescriptionMore.setVisibility(View.GONE);
                    }
                }
            });

    binding.experienceLocations.removeAllViews();
    for (final Availability availability : experience.getAvailabilities()) {
        if (availability.getName() != null && availability.getLat() != null && availability.getLon() != null) {
            final LocationItemBinding locationBinding = LocationItemBinding.inflate(getLayoutInflater(),
                    binding.experienceLocations, false);
            locationBinding.setAvailability(availability);
            locationBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Uri gmmIntentUri = Uri
                            .parse("geo:" + availability.getLat() + "," + availability.getLon());
                    final Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");
                    startActivity(mapIntent);
                }
            });
            binding.experienceLocations.addView(locationBinding.getRoot());
        }
    }

    if (experience.getOriginalID() != null) {
        getServer().loadExperience(experience.getOriginalID(), new LoadCallback<Experience>() {
            @Override
            public void loaded(Experience item) {
                originalExperience = item;
                binding.setOriginalExperience(item);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    if (updateActions()) {
        LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.hasExtra("experience")) {
                    loaded(new Gson().fromJson(intent.getStringExtra("experience"), Experience.class));
                }
            }
        }, new IntentFilter(getUri()));
    }
    updateStarred();
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Update layout according to {@link #mText} and span
 * @param width the specified width./*www.  j  a  va 2  s.c o m*/
 * @param forceWidth If true, force the text width to the specified width, otherwise, text width
 *                   may equals to or be smaller than the specified width.
 * @param previousLayout the result of previous layout, could be null.
 */
private @NonNull Layout createLayout(float width, boolean forceWidth, @Nullable Layout previousLayout) {
    float textWidth;
    textWidth = getTextWidth(mTextPaint, width, forceWidth);
    Layout layout;
    if (!FloatUtil.floatsEqual(previousWidth, textWidth) || previousLayout == null) {
        boolean forceRtl = false;
        Object direction = getStyles().get(Constants.Name.DIRECTION);
        if (direction != null && "text".equals(mType)) {
            forceRtl = direction.equals(Constants.Name.RTL);
        }
        layout = StaticLayoutProxy.create(spanned, mTextPaint, (int) Math.ceil(textWidth),
                Layout.Alignment.ALIGN_NORMAL, 1, 0, false, forceRtl);
    } else {
        layout = previousLayout;
    }
    if (mNumberOfLines != UNSET && mNumberOfLines > 0 && mNumberOfLines < layout.getLineCount()) {
        int lastLineStart, lastLineEnd;
        lastLineStart = layout.getLineStart(mNumberOfLines - 1);
        lastLineEnd = layout.getLineEnd(mNumberOfLines - 1);
        if (lastLineStart < lastLineEnd) {
            SpannableStringBuilder builder = null;
            if (lastLineStart > 0) {
                builder = new SpannableStringBuilder(spanned.subSequence(0, lastLineStart));
            } else {
                builder = new SpannableStringBuilder();
            }
            Editable lastLine = new SpannableStringBuilder(spanned.subSequence(lastLineStart, lastLineEnd));
            builder.append(truncate(lastLine, mTextPaint, (int) Math.ceil(textWidth), textOverflow));
            adjustSpansRange(spanned, builder);
            spanned = builder;
            return new StaticLayout(spanned, mTextPaint, (int) Math.ceil(textWidth),
                    Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        }
    }
    return layout;
}