M
M
MikkiMouse2015-08-09 12:32:08
Android
MikkiMouse, 2015-08-09 12:32:08

How to make text fade in TextView?

Good afternoon, I want to make a TextView like in Google Play (Books and Movies section), where it is used for description, with a Read more button. The image below is what I want to get.
7c1b8e8d7056425a96600239473bf604.png
How to make such letters at the end of TextView ?
I did it like this:
Markup:

<TextView
  android:id="@+id/main_text_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  
  android:maxLines="3" />
<TextView
  android:id="@+id/fading_text_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  
  android:fadingEdgeLength="32dp"
  android:requiresFadingEdge="horizontal"
  android:inputType="text" />

Some code:
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.my_layout, parent, false);

        TextView mainTextView = (TextView)v.findViewById(R.id.main_text_view);
        TextView fadingTextView = (TextView)v.findViewById(R.id.fading_text_view);

        mainTextView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right,
                                       int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (mainTextView.getLineCount() > 3) {
                    int start = mainTextView.getLayout().getLineStart(3);
                    String twoText = mainTextView.getText().toString().substring(start);
                    fadingTextView.setText(twoText);
                    mainTextView.setMaxLines(3);
                } else {
                    fadingTextView.setVisibility(View.GONE);
                }
            }
        });

        return v;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-08-09
@MikkiMouse

android:ellipsize="none"
android:fadingEdgeLength="16dp"
android:requiresFadingEdge="horizontal"

0607fa43a4904abab690e1e76118a563.JPG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question