I have a listView, one component of the row is a TextView. By default, I wish to show only 2 rows of text in the TextView. But if the user taps on the TextView I wanted to the textView to expand in context.
I actually have this portion working, but I want to have a more content indicator :

My current implementation (Below) has it’s own issues w/ not collapsing if the list view is scrolled, but I will handle that by storing the values for each cursor record in some collection..
I tried using chatBubble.getLineCount() but that returns zero initially b/c it has not gone through onLayout (from my understanding).
I only wish to show it if there is more than 2 lines of content.
I figure my solution will be creating my own implementation of TextView which can handle some of my requirements, but not sure if anyone has any examples I can look at.. (Or other suggestions).
<RelativeLayout
android:id="@+id/linear_layout_row_three"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linear_layout_row_two"
android:layout_toRightOf="@+id/munzeeQuickContact"
android:orientation="horizontal" >
<TextView
android:id="@+id/chat_bubble"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:background="@drawable/chat_bubble"
android:text="I went looking for this particular token on a rainy day, and I was unable to locate it due to the bad weather, next time please leave an um I went looking for this particular munzee on a rainy day, and I was unable to locate it due to the bad weather, next time please leave an um" />
<ImageView
android:id="@+id/minimize_maximize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/chat_bubble"
android:layout_alignRight="@id/chat_bubble"
android:visibility="gone"
android:src="@android:drawable/ic_menu_more"/>
</RelativeLayout>
Here is some of the source I currently have :
final TextView chatBubble = (TextView) view.getTag(R.id.chat_bubble);
final ViewGroup expandableContainer = (ViewGroup) view.getTag(R.id.linear_layout_row_three);
final ImageView minimizeMaximize = (ImageView) view.getTag(R.id.minimize_maximize);
chatBubble.setOnClickListener(
new View.OnClickListener() {
boolean isExpanded = false;
int lastHeight = 0;
// This is for the auto expanding text view
@Override
public void onClick(View v) {
if (isExpanded) {
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) expandableContainer
.getLayoutParams();
params.height = lastHeight;
chatBubble.setMaxLines(2);
expandableContainer.setLayoutParams(params);
expandableContainer.invalidate();
minimizeMaximize.setVisibility(View.VISIBLE);
} else {
lastHeight = expandableContainer.getHeight();
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) expandableContainer
.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
chatBubble.setMaxLines(99);
expandableContainer.setLayoutParams(params);
expandableContainer.invalidate();
minimizeMaximize.setVisibility(View.VISIBLE);
}
isExpanded = !isExpanded;
}
});
Have a look at the class below:
The
LimitedTextViewwill measure its text using its ownPaintobject and test it against the measured width. If it fits on the two allowed rows it will hide theImageView, otherwise it will show it. It also stores the current status of row(expanded/not-expanded) and increases or decreases the maximum number of lines to obtain the proper appearance.In the
getViewmethod of the adapter you would:set the status from a boolean array according to a position(this is also required to keep the rows in order as you scroll the list):
textView.storeCurrentStatus(mStatus[position])set the
OnClickListeneron theLimitedTextViewitself and from there update the status:mStatusboolean array you’ll probably change the drawable of theImageView, to show a different one depending on if theTextViewis expanded or notI manually wrote it, so there could be some mistakes I’m missing right now, take it as an idea. The
LimitedTextViewcould be improved as in performance, I also don’t know how well it would behave if you want to animate expanding the text.