Example usage for android.widget TextView setMaxLines

List of usage examples for android.widget TextView setMaxLines

Introduction

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

Prototype

@android.view.RemotableViewMethod
public void setMaxLines(int maxLines) 

Source Link

Document

Sets the height of the TextView to be at most maxLines tall.

Usage

From source file:Main.java

public static void animateTextViewMaxLinesChange(final TextView textView, final int maxLines,
        final int duration) {
    final int startHeight = textView.getMeasuredHeight();
    textView.setMaxLines(maxLines);
    textView.measure(View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    final int endHeight = textView.getMeasuredHeight();
    ObjectAnimator animation = ObjectAnimator.ofInt(textView, MAX_HEIGHT_ATTR, startHeight, endHeight);
    animation.addListener(new AnimatorListenerAdapter() {
        @Override//  w w w . ja va  2 s  .  c o m
        public void onAnimationEnd(Animator animation) {
            if (textView.getMaxHeight() == endHeight) {
                textView.setMaxLines(maxLines);
            }
        }
    }

    );
    animation.setDuration(duration).start();
}

From source file:com.kaedea.frontia.demo.DemoListFragment.java

@SuppressWarnings("ResourceType")
public static View getItemViewLayout(Context context) {
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setGravity(Gravity.CENTER_VERTICAL);
    int[] attrs = new int[] { R.attr.selectableItemBackground };
    TypedArray typedArray = context.obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    linearLayout.setBackgroundResource(backgroundResource);
    typedArray.recycle();//from   w  w  w.ja  va  2s.  c  o  m
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(layoutParams);
    // Title
    TextView tvTitle = new TextView(context);
    tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16f);
    tvTitle.setMaxLines(1);
    tvTitle.setTextColor(Color.parseColor("#212121"));
    tvTitle.setId(ID_TITLE);
    layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(Utils.dpToPx(context, 20f), Utils.dpToPx(context, 10f), Utils.dpToPx(context, 20f),
            0);
    linearLayout.addView(tvTitle, layoutParams);
    // Sub Title
    TextView tvSubTitle = new TextView(context);
    tvSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f);
    tvSubTitle.setMaxLines(2);
    tvSubTitle.setTextColor(Color.parseColor("#757575"));
    tvSubTitle.setId(ID_SUBTITLE);
    layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(Utils.dpToPx(context, 20f), 0, Utils.dpToPx(context, 20f),
            Utils.dpToPx(context, 10f));
    linearLayout.addView(tvSubTitle, layoutParams);
    return linearLayout;
}

From source file:Main.java

/**
 * Make a textview to a collapsible textview with the indicator on the right of the textview
 * /*w w  w  .  jav  a  2 s.c  o  m*/
 * @param tv , {@link TextView} to be converted
 * @param upDrawableResId , drawable resource id to be used as up indicator
 * @param downDrawableResId , drawable resource id to be used as down indicator
 * @param lineTreshold , no of line to be displayed for the collapsed state
 */
public static void makeCollapsible(final TextView tv, int upDrawableResId, int downDrawableResId,
        final int lineTreshold) {

    final Drawable[] drawables = tv.getCompoundDrawables();
    final Drawable up = tv.getContext().getResources().getDrawable(upDrawableResId);
    final Drawable down = tv.getContext().getResources().getDrawable(downDrawableResId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down, drawables[3]);
        tv.setEllipsize(TruncateAt.END);
        tv.setMaxLines(lineTreshold);
        tv.setTag(true);
        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    boolean snippet = (Boolean) tv.getTag();
                    if (snippet) {
                        // show everything
                        snippet = false;
                        tv.setMaxLines(Integer.MAX_VALUE);
                        tv.setEllipsize(null);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], up,
                                drawables[3]);
                    } else {
                        // show snippet
                        snippet = true;
                        tv.setMaxLines(lineTreshold);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                    }
                    tv.setTag(snippet);
                }

            }
        });
    } else {
        tv.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

                ViewTreeObserver vto = tv.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {

                        tv.setEllipsize(TruncateAt.END);
                        int line = tv.getLineCount();
                        tv.setMaxLines(lineTreshold);
                        if (line <= lineTreshold) {
                            tv.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    // empty listener
                                    // Log.d("line count", "count: "+
                                    // tv.getLineCount());
                                }
                            });
                            if (tv.getLayout() != null) {
                                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                } else {
                                    tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                }
                            }
                            return;
                        }

                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], down,
                                drawables[3]);
                        tv.setTag(true);
                        tv.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (v instanceof TextView) {
                                    TextView tv = (TextView) v;
                                    boolean snippet = (Boolean) tv.getTag();
                                    if (snippet) {
                                        snippet = false;
                                        // show everything
                                        tv.setMaxLines(Integer.MAX_VALUE);
                                        tv.setEllipsize(null);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                up, drawables[3]);
                                    } else {
                                        snippet = true;
                                        // show snippet
                                        tv.setMaxLines(lineTreshold);
                                        tv.setEllipsize(TruncateAt.END);
                                        tv.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1],
                                                down, drawables[3]);
                                    }
                                    tv.setTag(snippet);
                                }

                            }
                        });

                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } else {
                            tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                    }

                });
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

        });
    }

}

From source file:com.miz.utils.ViewUtils.java

/**
 * Animates the transition when changing the maxLines
 * attribute of a TextView./*  w  ww.j av  a 2 s  . c o  m*/
 * @param text
 * @param maxLines
 */
public static void animateTextViewMaxLines(TextView text, int maxLines) {
    try {
        ObjectAnimator animation = ObjectAnimator.ofInt(text, "maxLines", maxLines);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);
        animation.start();
    } catch (Exception e) {
        // Some devices crash at runtime when using the ObjectAnimator
        text.setMaxLines(maxLines);
    }
}

From source file:com.desno365.mods.DesnoUtils.java

public static void expandTextView(ViewGroup container, TextView tv) {
    // animation//from  w  ww  . j a va2  s. c  o m
    if (Build.VERSION.SDK_INT >= 19) {
        TransitionManager.beginDelayedTransition(container, new AutoTransition()
                .setDuration(SharedConstants.CHANGELOG_TEXT_ANIMATION_DURATION_PER_LINE * tv.getLineCount()));
    }

    // expand the TextView with setMaxLines
    tv.setMaxLines(Integer.MAX_VALUE);
}

From source file:com.android.settings.widget.RadioButtonPreference.java

@Override
public void onBindViewHolder(PreferenceViewHolder view) {
    super.onBindViewHolder(view);

    TextView title = (TextView) view.findViewById(android.R.id.title);
    if (title != null) {
        title.setSingleLine(false);//from w  w w.  ja va  2  s  .co  m
        title.setMaxLines(3);
    }
}

From source file:com.creationgroundmedia.popularmovies.reviews.ReviewFragment.java

@Override
public void onLoadFinished(Loader<List<ReviewItem>> loader, final List<ReviewItem> data) {
    if (data != null) {
        for (ReviewItem review : data) {
            View tv = LayoutInflater.from(getContext()).inflate(R.layout.fragment_review, null);
            ((LinearLayout) mView).addView(tv);
            TextView authorView = (TextView) tv.findViewById(R.id.author);
            authorView.setText(String.format("Review by %s", review.getAuthor()));
            final TextView contentView = (TextView) tv.findViewById(R.id.content);
            contentView.setMaxLines(PREVIEW_LINES);
            contentView.setEllipsize(TextUtils.TruncateAt.END);
            contentView.setSelected(false);
            contentView.setText(review.getContent());
            tv.setOnClickListener(new View.OnClickListener() {
                @Override/*  ww w.  j  a  v  a 2 s  .  co  m*/
                public void onClick(View v) {
                    if (contentView.isSelected()) {
                        contentView.setEllipsize(TextUtils.TruncateAt.END);
                        contentView.setMaxLines(PREVIEW_LINES);
                        contentView.setSelected(false);
                    } else {
                        contentView.setEllipsize(null);
                        contentView.setMaxLines(Integer.MAX_VALUE);
                        contentView.setSelected(true);
                    }
                }
            });
        }
        getLoaderManager().destroyLoader(URL_REVIEWLOADER);
    }
}

From source file:com.baruckis.nanodegree.spotifystreamer.activities.BaseActivity.java

public void showSpotifyAuthenticateSnackbar() {
    Snackbar snackbar = Snackbar.make(findViewById(R.id.artists_list),
            R.string.snackbar_spotify_authenticate_msg_error, Snackbar.LENGTH_INDEFINITE);
    View snackbarView = snackbar.getView();
    TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setMaxLines(3);
    snackbar.setAction(R.string.snackbar_spotify_authenticate_msg_action, new View.OnClickListener() {
        @Override//from  ww  w.j  a  v a  2 s  .c  om
        public void onClick(View v) {
            requestSpotifyApiToken();
        }
    });
    snackbar.show();
}

From source file:smikhlevskiy.uafinance.ui.wigets.SlidingTabLayout.java

/**
 * Create aaaa default view to be used for tabs. This is called if aaaa custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//  ww  w  .  java  2 s. co m
 */
private TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setMaxLines(1);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    textView.setBackgroundResource(android.R.color.transparent);

    if (android.os.Build.VERSION.SDK_INT > 10) {
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    textView.setTextColor(textColor);

    if (android.os.Build.VERSION.SDK_INT > 13) {
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

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  .  ja v  a  2 s  .c om*/
        }
    }

    tvLog.setMaxLines(lines);
}