Skip to content

Instantly share code, notes, and snippets.

@Kane-Shih
Last active August 29, 2015 14:22
Show Gist options
  • Save Kane-Shih/e8a6caba37f445e9ecbe to your computer and use it in GitHub Desktop.
Save Kane-Shih/e8a6caba37f445e9ecbe to your computer and use it in GitHub Desktop.
package tw.kaneshih.view;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import tw.kaneshih.app.R;
public final class ExpandableTextView extends LinearLayout {
private TextView content;
private View button;
private TextView buttonText;
private ImageView buttonImage;
private boolean isExpanded = false;
private int collapsedLines = 2;
private int drawableIdForExpand = R.drawable.ic_introduction_closed;
private int drawableIdForCollapse = R.drawable.ic_introduction_open;
private int stringIdForExpand = R.string.show_less;
private int stringIdForCollapse = R.string.show_more;
public ExpandableTextView(Context context) {
super(context);
initLayout(context);
}
public ExpandableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initLayout(context);
}
public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initLayout(context);
}
private void initLayout(Context context) {
LayoutInflater.from(context).inflate(R.layout.expandable_textview, this);
content = (TextView) findViewById(R.id.expandable_textview_text);
button = findViewById(R.id.expandable_textview_button);
buttonText = (TextView) findViewById(R.id.expandable_textview_button_text);
buttonImage = (ImageView) findViewById(R.id.expandable_textview_button_image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isExpanded) {
content.setMaxLines(collapsedLines);
buttonText.setText(stringIdForCollapse);
buttonImage.setImageResource(drawableIdForCollapse);
} else {
content.setMaxLines(Integer.MAX_VALUE);
buttonText.setText(stringIdForExpand);
buttonImage.setImageResource(drawableIdForExpand);
}
isExpanded = !isExpanded;
content.scrollTo(0, 0);
}
});
}
public void setText(String text) {
content.setText(text);
}
/**
* How many lines to show while this is collapsed?
*
* @param collapsedLines
* - default is 2
*/
public void setCollapsedLines(int collapsedLines) {
this.collapsedLines = collapsedLines;
}
public TextView getContentTextView() {
return content;
}
/**
* the arrow which at right side of button text
*
* @param forCollapse
* @param forExpand
*/
public void setResourceDrawableForButtonIndicator(@DrawableRes int forCollapse, @DrawableRes int forExpand) {
this.drawableIdForCollapse = forCollapse;
this.drawableIdForExpand = forExpand;
}
public void setResourceStringForButtonText(@StringRes int forCollapse, @StringRes int forExpand) {
this.stringIdForCollapse = forCollapse;
this.stringIdForExpand = forExpand;
}
public boolean isExpanded() {
return isExpanded;
}
}
/* XML R.layout.expandable_textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/expandable_textview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/expandable_textview_text"
android:textSize="@dimen/expandable_textview_textsize"
android:textStyle="bold"/>
<LinearLayout
android:id="@+id/expandable_textview_button"
android:layout_width="match_parent"
android:layout_height="@dimen/expandable_textview_button_height"
android:background="@drawable/bg_expandable_textview_button"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/expandable_textview_button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/expandable_textview_button_text_margin_right"
android:text="@string/show_more"
android:textColor="@color/expandable_textview_button_text"
android:textSize="@dimen/expandable_textview_button_textsize"
android:textStyle="bold"/>
<ImageView
android:id="@+id/expandable_textview_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_introduction_open"/>
</LinearLayout>
</LinearLayout>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment