Answer the question
In order to leave comments, you need to log in
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.
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" />
@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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question