Example usage for android.widget TextView getMaxLines

List of usage examples for android.widget TextView getMaxLines

Introduction

In this page you can find the example usage for android.widget TextView getMaxLines.

Prototype

public int getMaxLines() 

Source Link

Document

Returns the maximum height of TextView in terms of number of lines or -1 if the maximum height was set using #setMaxHeight(int) or #setHeight(int) .

Usage

From source file:Main.java

private static int getMaxLines(TextView view) {
    int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)

    TransformationMethod method = view.getTransformationMethod();
    if (method != null && method instanceof SingleLineTransformationMethod) {
        maxLines = 1;/*from   w  w w  .  j  a  va2  s. co  m*/
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // setMaxLines() and getMaxLines() are only available on android-16+
        maxLines = view.getMaxLines();
    }

    return maxLines;
}

From source file:cn.dreamtobe.emoji.ellipsize.helper.SpanEllipsizeEndHelper.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static CharSequence matchMaxWidth(SpannableString targetText, TextView textView) {
    if (targetText.length() <= 0) {
        return targetText;
    }//from  w  ww . j a  v a2  s.c o m

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        return targetText;
    }

    if (textView == null) {
        return targetText;
    }

    final int maxWidth = textView.getMaxWidth();

    if (maxWidth <= 0 || maxWidth >= Integer.MAX_VALUE) {
        return targetText;
    }

    if (textView.getEllipsize() != TextUtils.TruncateAt.END) {
        return targetText;
    }

    //TODO Multi-lines support
    if (textView.getMaxLines() != 1) {
        return targetText;
    }

    final String maxWidthKey = getMaxWidthKey(targetText, textView);
    SpannableString tmpText = SPAN_MAXWIDTH_CACHE.get(maxWidthKey);
    if (tmpText != null) {
        removeClickableSpan(tmpText);
        return tmpText;
    }

    TextPaint textPaint = textView.getPaint();
    if (textPaint == null) {
        return targetText;
    }

    final int totalWidth = (int) textPaint.measureText(targetText, 0, targetText.length());
    if (totalWidth <= maxWidth) {
        return targetText;
    }

    final long startTime = System.currentTimeMillis();
    // deal maxwitdh

    final int dotWidth = (int) textPaint.measureText("...");

    tmpText = targetText;

    int start = 0;
    int end = targetText.length();

    // targetX is maxWidth - "...".length
    int targetX = maxWidth - dotWidth;

    //dichotomy: get x most touch targetX
    int middle = targetText.length();
    int x = 0;
    while (start <= end) {
        // tx = targetX, tl = targetLength

        // width:  0           x
        // length: 0         middle           end
        //         -------------|-------------
        middle = (start + end) / 2;

        int emojiDraW = 0;
        int emojiStrW = 0;

        int emojiExcursion = 1;

        final Object[] tmpSpans = tmpText.getSpans(0, middle, Object.class);
        if (tmpSpans != null) {
            for (Object tmpSpan : tmpSpans) {
                final int tmpStart = tmpText.getSpanStart(tmpSpan);
                final int tmpEnd = tmpText.getSpanEnd(tmpSpan);

                //middle in (tmpStart, tmpEnd)
                if (tmpStart < middle && tmpEnd > middle) {
                    middle = tmpEnd;
                    emojiExcursion = tmpEnd - tmpStart;
                }
            }

            // TextPaint#measure do not attention span, so adjust by ourselves
            for (Object tmpSpan : tmpSpans) {
                final int tmpStart = tmpText.getSpanStart(tmpSpan);
                final int tmpEnd = tmpText.getSpanEnd(tmpSpan);

                // TODO support other span
                if (tmpStart < middle && tmpSpan instanceof ImageSpan) {
                    emojiDraW += ((ImageSpan) tmpSpan).getDrawable().getBounds().width();
                    emojiStrW += textPaint.measureText(tmpText, tmpStart, tmpEnd);
                }
            }

        }

        x = (int) textPaint.measureText(tmpText, 0, middle);
        x = x - emojiStrW + emojiDraW;

        //            x = (int) (textPaint.measureText(pureStr, 0, pureStr.length()) + emojiWidth);

        //            Log.d(TAG, String.format("targetX: %d, currentX: %d, currentLength: %d, totalLength: %d, emojiStrW[%d], emojiDraW[%d]", targetX, x, middle, targetText.length(), emojiStrW, emojiDraW));

        if (x > targetX) {
            // width:  0       tx        x
            // length: start   tl      middle         end
            //             ----|---------|-------------
            // TO:     start   |       *end
            //             ----|--------|--------------
            end = middle - emojiExcursion;
        } else if (x < targetX) {
            // width:  0               x       tx
            // length: start         middle    tl     end
            //           --------------|-------|------
            // TO:                      *start  |       end
            //           ---------------|------|------
            start = middle + 1;
        } else {
            break;
        }
    }

    // adjust x larger targetX
    while (x > targetX && middle > 0) {
        x = (int) textPaint.measureText(tmpText, 0, --middle);
    }

    // adjust x middle emoji span
    final Object[] ajustSpans = tmpText.getSpans(0, tmpText.length(), Object.class);
    for (Object adjustSpan : ajustSpans) {
        final int adjustStart = tmpText.getSpanStart(adjustSpan);
        final int adjustEnd = tmpText.getSpanEnd(adjustSpan);

        //[adjustStart, adjustEnd)
        if (middle >= adjustStart && middle < adjustEnd) {
            middle = adjustStart - 1;
            break;
        }
    }

    // finnal middle

    // sub sequence [0, middle + 1) & remove [middle +1, length] spans
    tmpText = (SpannableString) tmpText.subSequence(0, middle + 1);
    //        Log.d(TAG, String.format("sub Sequence[0, %d), [%s] to [%s]", middle + 1, targetText, tmpText));

    // add ...
    final SpannableString maxWidthSS = new SpannableString(tmpText + "...");

    final Object[] maxWidthSpans = tmpText.getSpans(0, tmpText.length(), Object.class);
    if (maxWidthSpans != null) {
        for (Object maxWidthSpan : maxWidthSpans) {
            final int mwSpanStart = tmpText.getSpanStart(maxWidthSpan);
            final int mwSpanEnd = tmpText.getSpanEnd(maxWidthSpan);
            final int mwSpanFlag = tmpText.getSpanFlags(maxWidth);

            maxWidthSS.setSpan(maxWidthSpan, mwSpanStart, mwSpanEnd, mwSpanFlag);
        }
    }

    targetText = maxWidthSS;

    SPAN_MAXWIDTH_CACHE.put(maxWidthKey, targetText);
    Log.d(TAG, String.format("deal maxWidth %d", System.currentTimeMillis() - startTime));

    return targetText;
}

From source file:org.comixwall.pffw.NotificationDetails.java

@Override
public void onItemClick(View view) {

    TextView tvLog = view.findViewById(R.id.log);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvLog.getMaxLines() != 1) {
            lines = 1;//from w  w  w.  j av  a  2s.c om
        }
    }

    tvLog.setMaxLines(lines);
}

From source file:me.albinmathew.celluloid.ui.fragments.MovieDetailFragment.java

@Override
public void onClick(@NonNull View view) {
    {/*w ww  .  j a v  a  2s.  c  o m*/
        switch (view.getId()) {
        case R.id.video_thumb:
            String videoUrl = (String) view.getTag();
            Intent playVideoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
            startActivity(playVideoIntent);
            break;

        case R.id.review_content:
            TextView review = (TextView) view;
            if (review.getMaxLines() == 5) {
                review.setMaxLines(500);
            } else {
                review.setMaxLines(5);
            }
            break;
        case R.id.fab:
            setFavourites();
        default:
            break;
        }
    }
}

From source file:org.comixwall.pffw.InfoRules.java

@Override
public void onItemClick(View view) {

    TextView tvRule = view.findViewById(R.id.rule);
    TextView tvEvalsStates = view.findViewById(R.id.evalsStates);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvRule.getMaxLines() != 1) {
            lines = 1;/*w w  w .  j a  va2 s.c o  m*/
        }
    }

    tvRule.setMaxLines(lines);
    tvEvalsStates.setMaxLines(lines);
}

From source file:org.comixwall.pffw.Notifications.java

@Override
public void onItemClick(View view) {

    TextView tvTitle = view.findViewById(R.id.title);
    TextView tvBody = view.findViewById(R.id.body);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvTitle.getMaxLines() != 1) {
            lines = 1;//from  www.jav a 2s.  c om
        }
    }

    tvTitle.setMaxLines(lines);
    tvBody.setMaxLines(lines);

    List<NotificationDetail> details = new ArrayList<>();

    // Position == view id
    Notification notification = mNotificationsList.get(view.getId());
    try {
        JSONObject jsonArray = new JSONObject(notification.data);

        for (String p : priorities) {
            Iterator<String> modules = jsonArray.keys();
            while (modules.hasNext()) {
                String module = modules.next();
                Iterator<String> prios = jsonArray.getJSONObject(module).keys();
                while (prios.hasNext()) {
                    String prio = prios.next();
                    if (prio.contains(p)) {
                        JSONArray logs = jsonArray.getJSONObject(module).getJSONArray(prio);
                        // There is only one sample log record, at index 0
                        details.add(NotificationDetail.newInstance(module, logs.getJSONObject(0)));
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.warning("onItemClick exception= " + e.toString());
    }

    NotificationDetails.setNotificationDetails(details);

    // TODO: Code reuse, unite with MainActivity onNavigationItemSelected()
    fragment = new NotificationDetails();

    Bundle args = new Bundle();
    args.putString("title", notification.title);
    args.putString("body", notification.body);
    args.putString("datetime", notification.datetime);

    fragment.setArguments(args);

    FragmentManager fm = getActivity().getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    String fragmentName = fragment.getClass().getSimpleName();
    transaction.addToBackStack(fragmentName);

    transaction.replace(R.id.fragmentContainer, fragment);
    transaction.commit();

    ((MainActivity) getActivity()).createOptionsMenu();
}

From source file:org.comixwall.pffw.InfoQueues.java

@Override
public void onItemClick(View view) {

    TextView tvPacketsBytes = view.findViewById(R.id.packetsBytes);
    TextView tvDropped = view.findViewById(R.id.dropped);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvPacketsBytes.getMaxLines() != 1) {
            lines = 1;// w  w  w  .  ja  v a  2  s.  c om
        }
    }

    tvPacketsBytes.setMaxLines(lines);
    tvDropped.setMaxLines(lines);
}

From source file:org.comixwall.pffw.Dashboard.java

@Override
public void onItemClick(View view) {

    TextView tvModule = view.findViewById(R.id.module);
    TextView tvErrors = view.findViewById(R.id.errors);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvModule.getMaxLines() != 1) {
            lines = 1;/*from   w  w  w.j  a v  a2s . c o  m*/
        }
    }

    tvModule.setMaxLines(lines);
    tvErrors.setMaxLines(lines);
}

From source file:org.comixwall.pffw.InfoIfs.java

@Override
public void onItemClick(View view) {

    TextView tvName = view.findViewById(R.id.name);
    TextView tvCleared = view.findViewById(R.id.cleared);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvName.getMaxLines() != 1) {
            lines = 1;/*  w  w  w.  ja va 2s  .  c  o m*/
        }
    }

    tvName.setMaxLines(lines);
    tvCleared.setMaxLines(lines);
}

From source file:org.comixwall.pffw.InfoSystem.java

@Override
public void onItemClick(View view) {
    TextView tvCommand = view.findViewById(R.id.command);
    TextView tvOthers = view.findViewById(R.id.others);

    int lines = 10;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        if (tvCommand.getMaxLines() != 1) {
            lines = 1;/*from  www.j a va  2 s  .  c o m*/
        }
    }

    tvCommand.setMaxLines(lines);
    tvOthers.setMaxLines(lines);
}