Example usage for android.text Layout getLineCount

List of usage examples for android.text Layout getLineCount

Introduction

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

Prototype

public abstract int getLineCount();

Source Link

Document

Return the number of lines of text in this layout.

Usage

From source file:Main.java

/**
 * Returns true if the textview is ellipsized
 * @param textView/*from   w  w w  .j a  v a 2 s  .  c o m*/
 * @return
 */
public static boolean isTextEllipsized(TextView textView) {

    Layout textViewLayout = textView.getLayout();

    if (textViewLayout != null) {

        int lines = textViewLayout.getLineCount();

        if (lines > 0) {

            if (textViewLayout.getEllipsisCount(lines - 1) > 0)
                return true;
        }
    }

    return false;
}

From source file:free.yhc.feeder.model.Utils.java

/**
 * Text in given TextView is ellipsed?/*from  ww  w.  jav a  2s.c  om*/
 * @param tv
 * @return
 */
public static boolean isEllipsed(TextView tv) {
    Layout l = tv.getLayout();
    if (null != l) {
        int lines = l.getLineCount();
        if (lines > 0)
            if (l.getEllipsisCount(lines - 1) > 0)
                return true;
    }
    return false;
}

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

/**
 * Which line in the layout is the top visible line.
 *
 * @param layout Calculated text layout.
 * @param bounds Visible bounds in the coordinates of the TextView and layout.
 * @return The top visible line./*from   w ww.jav  a2 s .  co m*/
 */
private int topVisibleLineOfText(final Layout layout, final Rect bounds) {

    final int lineCount = layout.getLineCount();
    for (int lineNum = 0; lineNum < lineCount; lineNum++) {
        final int top = layout.getLineTop(lineNum);
        if (top >= bounds.top && top <= bounds.bottom) {
            return lineNum;
        }
    }
    return lineCount;
}

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

/**
 * Which line in the layout is the bottom visible line.
 *
 * @param layout Calculated text layout.
 * @param bounds Visible bounds in the coordinates of the TextView and layout.
 * @return The bottom visible line//w ww  .jav  a  2 s .  com
 */
private int bottomVisibleLineOfText(final Layout layout, final Rect bounds) {

    final int lineCount = layout.getLineCount();
    for (int lineNum = lineCount - 1; lineNum >= 0; lineNum--) {
        final int bottom = layout.getLineBottom(lineNum);
        if (bottom >= bounds.top && bottom <= bounds.bottom) {
            return lineNum;
        }
    }
    return 0;
}

From source file:org.kontalk.ui.view.TextContentView.java

private float getMaxLineWidth(Layout layout) {
    float max_width = 0.0f;
    int lines = layout.getLineCount();
    for (int i = 0; i < lines; i++) {
        if (layout.getLineWidth(i) > max_width) {
            max_width = layout.getLineWidth(i);
        }//from  ww  w .  j a  v a2 s  .co  m
    }
    return max_width;
}

From source file:org.kontalk.ui.view.TextContentView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (mMeasureHack) {
        int specModeW = MeasureSpec.getMode(widthMeasureSpec);
        if (specModeW != MeasureSpec.EXACTLY) {
            Layout layout = getLayout();
            int linesCount = layout.getLineCount();
            if (linesCount > 1) {
                float textRealMaxWidth = 0;
                for (int n = 0; n < linesCount; ++n) {
                    textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n));
                }/*from w w  w  . j  a v a  2 s  .c o  m*/
                int w = Math.round(textRealMaxWidth);
                if (w < getMeasuredWidth()) {
                    super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), heightMeasureSpec);
                }
            }
        }
    }
}

From source file:com.facebook.litho.widget.TextSpec.java

@OnMeasure
static void onMeasure(ComponentContext context, ComponentLayout layout, int widthSpec, int heightSpec,
        Size size, @Prop(resType = ResType.STRING) CharSequence text,
        @Prop(optional = true) TruncateAt ellipsize,
        @Prop(optional = true, resType = ResType.BOOL) boolean shouldIncludeFontPadding,
        @Prop(optional = true, resType = ResType.INT) int minLines,
        @Prop(optional = true, resType = ResType.INT) int maxLines,
        @Prop(optional = true, resType = ResType.INT) int minEms,
        @Prop(optional = true, resType = ResType.INT) int maxEms,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int minWidth,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int maxWidth,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
        @Prop(optional = true, resType = ResType.COLOR) int shadowColor,
        @Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine,
        @Prop(optional = true, resType = ResType.COLOR) int textColor,
        @Prop(optional = true) ColorStateList textColorStateList,
        @Prop(optional = true, resType = ResType.COLOR) int linkColor,
        @Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float extraSpacing,
        @Prop(optional = true, resType = ResType.FLOAT) float spacingMultiplier,
        @Prop(optional = true) int textStyle, @Prop(optional = true) Typeface typeface,
        @Prop(optional = true) Alignment textAlignment, @Prop(optional = true) boolean glyphWarming,
        @Prop(optional = true) TextDirectionHeuristicCompat textDirection, Output<Layout> measureLayout,
        Output<Integer> measuredWidth, Output<Integer> measuredHeight) {

    if (TextUtils.isEmpty(text)) {
        measureLayout.set(null);//  ww  w. ja v  a2  s.co  m
        size.width = 0;
        size.height = 0;
        return;
    }

    Layout newLayout = createTextLayout(widthSpec, ellipsize, shouldIncludeFontPadding, maxLines, shadowRadius,
            shadowDx, shadowDy, shadowColor, isSingleLine, text, textColor, textColorStateList, linkColor,
            textSize, extraSpacing, spacingMultiplier, textStyle, typeface, textAlignment, glyphWarming,
            layout.getResolvedLayoutDirection(), minEms, maxEms, minWidth, maxWidth, textDirection);

    measureLayout.set(newLayout);

    size.width = SizeSpec.resolveSize(widthSpec, newLayout.getWidth());

    // Adjust height according to the minimum number of lines.
    int preferredHeight = LayoutMeasureUtil.getHeight(newLayout);
    final int lineCount = newLayout.getLineCount();
    if (lineCount < minLines) {
        final TextPaint paint = newLayout.getPaint();

        final int lineHeight = Math.round(paint.getFontMetricsInt(null) * spacingMultiplier + extraSpacing);
        preferredHeight += lineHeight * (minLines - lineCount);
    }

    size.height = SizeSpec.resolveSize(heightSpec, preferredHeight);

    // Some devices seem to be returning negative sizes in some cases.
    if (size.width < 0 || size.height < 0) {
        size.width = Math.max(size.width, 0);
        size.height = Math.max(size.height, 0);

        final ComponentsLogger logger = context.getLogger();
        if (logger != null) {
            final LogEvent event = logger.newEvent(EVENT_ERROR);
            event.addParam(PARAM_MESSAGE, "Text layout measured to less than 0 pixels");
            logger.log(event);
        }
    }

    measuredWidth.set(size.width);
    measuredHeight.set(size.height);
}

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);// w w w. ja v a2  s.  c  om

    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.coreform.open.android.formidablevalidation.SetErrorHandler.java

private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
    int wid = tv.getPaddingLeft() + tv.getPaddingRight();
    int ht = tv.getPaddingTop() + tv.getPaddingBottom();

    //com.android.internal.R.dimen.textview_error_popup_default_width introduced after Gingerbread, only has one variant (240dip)
    int defaultWidthInPixels = mContext.getResources()
            .getDimensionPixelSize(R.dimen.textview_error_popup_default_width);
    Layout l = new StaticLayout(text, tv.getPaint(), defaultWidthInPixels, Layout.Alignment.ALIGN_NORMAL, 1, 0,
            true);// ww  w .j av  a  2 s  .com

    float max = 0;
    for (int i = 0; i < l.getLineCount(); i++) {
        max = Math.max(max, l.getLineWidth(i));
    }

    if (DEBUG)
        Log.d(TAG, "max: " + max + ", height: " + l.getHeight());

    /*
     * Now set the popup size to be big enough for the text plus the border.
     */
    pop.setWidth(wid + (int) Math.ceil(max));
    pop.setHeight(ht + l.getHeight()); //TODO: buggy (the 2 shouldnt need to be there)
}

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

/**
 * Update layout according to {@link #mText} and span
 * @param width the specified width./*from   w ww.  j a  v a  2s  .c om*/
 * @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;
}