Skip to content

Commit

Permalink
Merge pull request #3 from Manabu-GT/v0.1.1
Browse files Browse the repository at this point in the history
bug fixes for v0.1.1
  • Loading branch information
Manabu-GT committed Nov 9, 2014
2 parents f539458 + 1d679ab commit 598fd2f
Show file tree
Hide file tree
Showing 18 changed files with 410 additions and 144 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ ExpandableTextView is an Android library that allows developers to easily create
which can expand/collapse just like the Google Play's app description.
Feel free to use it all you want in your Android apps provided that you cite this project.

<img src="https://raw.github.com/Manabu-GT/ExpandableTextView/master/art/readme_demo.gif" width=346 height=588 alt="Quick Demo">
<img src="https://raw.github.com/Manabu-GT/ExpandableTextView/master/art/readme_demo.gif" width=346 height=549 alt="Quick Demo">

Requirements
-------------
API Level 8 (Froyo) and above.

Setup
------
You just need to add the followings to your ***build.gradle*** file:
The library is pushed to Maven Central as an AAR,
so you just need to add the followings to your ***build.gradle*** file:

```
repositories {
maven { url 'http://Manabu-GT.github.com/ExpandableTextView/mvn-repo' }
}
dependencies {
compile 'com.ms.square:expandabletextview:0.1.0'
compile 'com.ms-square:expandabletextview:0.1.1'
}
```

Usage
------
Using the library is really simple, just look at the source code of the [provided sample][1].
(Look at the SampleTextListAdapter.java for the use within a ListView)

Also, you can optionally set the following attributes in your layout xml file to customize the behavior
of the ExpandableTextView.
Expand All @@ -40,6 +40,8 @@ of the ExpandableTextView.

* `animAlphaStart` (defaults to 0.7f)
Alpha value of the TextView when the animation starts
(NOTE)
Set this value to 1 if you want to disable the alpha animation.

* `expandDrawable`
Customize a drawable set to ImageButton to expand the TextView
Expand Down
Binary file modified art/readme_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 0 additions & 37 deletions build-gh-pages.sh

This file was deleted.

11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath 'com.android.tools.build:gradle:0.12.+'
}
}

Expand All @@ -14,3 +14,12 @@ allprojects {
mavenCentral()
}
}

// http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html
project.ext {
ANDROID_COMPILE_SDK_VERSION = 21
ANDROID_BUILD_TOOLS_VERSION = "21.1.1"

ANDROID_MIN_SDK_VERSION = 8
ANDROID_TARGET_SDK_VERSION = 21
}
29 changes: 7 additions & 22 deletions lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'android-library'
apply plugin: 'maven'

android {
compileSdkVersion 19
buildToolsVersion "19.0.3"

compileSdkVersion project.ANDROID_COMPILE_SDK_VERSION
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 8
targetSdkVersion 19
minSdkVersion project.ANDROID_MIN_SDK_VERSION
targetSdkVersion project.ANDROID_TARGET_SDK_VERSION
}
buildTypes {
release {
Expand All @@ -21,20 +21,5 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

def repositoryUrl
if (has('repoDir')) {
repositoryUrl = "file://$repoDir/mvn-repo/"
} else {
repositoryUrl = "file://$buildDir/mvn-repo/"
}

uploadArchives {
repositories {
mavenDeployer {
repository(url: repositoryUrl)
pom.version = '0.1.0'
pom.groupId = 'com.ms.square'
pom.artifactId = 'expandabletextview'
}
}
}
// for maven central deployment
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;
Expand All @@ -36,13 +39,13 @@ public class ExpandableTextView extends LinearLayout implements View.OnClickList

private static final String TAG = ExpandableTextView.class.getSimpleName();

// The default number of lines;
/* The default number of lines */
private static final int MAX_COLLAPSED_LINES = 8;

// The default animation duration
/* The default animation duration */
private static final int DEFAULT_ANIM_DURATION = 300;

// The default alpha value when the animation starts
/* The default alpha value when the animation starts */
private static final float DEFAULT_ANIM_ALPHA_START = 0.7f;

protected TextView mTv;
Expand All @@ -55,7 +58,7 @@ public class ExpandableTextView extends LinearLayout implements View.OnClickList

private int mCollapsedHeight;

private int mMeasuredTextHeight;
private int mTextHeightWithMaxLines;

private int mMaxCollapsedLines;

Expand All @@ -69,6 +72,10 @@ public class ExpandableTextView extends LinearLayout implements View.OnClickList

private float mAnimAlphaStart;

/* For saving collapsed status when used in ListView */
private SparseBooleanArray mCollapsedStatus;
private int mPosition;

public ExpandableTextView(Context context) {
super(context);
}
Expand All @@ -93,12 +100,16 @@ public void onClick(View view) {
mCollapsed = !mCollapsed;
mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable);

if (mCollapsedStatus != null) {
mCollapsedStatus.put(mPosition, mCollapsed);
}

Animation animation;
if (mCollapsed) {
animation = new ExpandCollapseAnimation(this, getHeight(), mCollapsedHeight);
} else {
animation = new ExpandCollapseAnimation(this, getHeight(), getHeight() +
mMeasuredTextHeight - mTv.getHeight());
mTextHeightWithMaxLines - mTv.getHeight());
}

animation.setFillAfter(true);
Expand All @@ -117,6 +128,11 @@ public void onAnimationRepeat(Animation animation) { }
startAnimation(animation);
}

@Override
protected void onFinishInflate() {
findViews();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// If no change, measure and return
Expand All @@ -140,7 +156,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}

// Saves the text height w/ max lines
mMeasuredTextHeight = mTv.getMeasuredHeight();
mTextHeightWithMaxLines = getRealTextViewHeight(mTv);

// Doesn't fit in collapsed mode. Collapse text view as needed. Show
// button.
Expand Down Expand Up @@ -208,14 +224,22 @@ private static void applyAlphaAnimation(View view, float alpha) {
}
}

public void setText(String text) {
public void setText(CharSequence text) {
mRelayout = true;
if (mTv == null) {
findViews();
}
String trimmedText = text.trim();
mTv.setText(trimmedText);
setVisibility(trimmedText.length() == 0 ? View.GONE : View.VISIBLE);
mTv.setText(text);
setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
}

public void setText(CharSequence text, SparseBooleanArray collapsedStatus, int position) {
mCollapsedStatus = collapsedStatus;
mPosition = position;
boolean isCollapsed = collapsedStatus.get(position, true);
clearAnimation();
mCollapsed = isCollapsed;
mButton.setImageDrawable(mCollapsed ? mExpandDrawable : mCollapseDrawable);
setText(text);
getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
requestLayout();
}

public CharSequence getText() {
Expand All @@ -225,6 +249,12 @@ public CharSequence getText() {
return mTv.getText();
}

private static int getRealTextViewHeight(TextView textView) {
int textHeight = textView.getLayout().getLineTop(textView.getLineCount());
int padding = textView.getCompoundPaddingTop() + textView.getCompoundPaddingBottom();
return textHeight + padding;
}

protected class ExpandCollapseAnimation extends Animation {
private final View mTargetView;
private final int mStartHeight;
Expand All @@ -241,7 +271,9 @@ public ExpandCollapseAnimation(View view, int startHeight, int endHeight) {
protected void applyTransformation(float interpolatedTime, Transformation t) {
final int newHeight = (int)((mEndHeight - mStartHeight) * interpolatedTime + mStartHeight);
mTv.setMaxHeight(newHeight - mMarginBetweenTxtAndBottom);
applyAlphaAnimation(mTv, mAnimAlphaStart + interpolatedTime * (1.0f - mAnimAlphaStart));
if (Float.compare(mAnimAlphaStart, 1.0f) != 0) {
applyAlphaAnimation(mTv, mAnimAlphaStart + interpolatedTime * (1.0f - mAnimAlphaStart));
}
mTargetView.getLayoutParams().height = newHeight;
mTargetView.requestLayout();
}
Expand Down
14 changes: 8 additions & 6 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
apply plugin: 'android'

android {
compileSdkVersion 19
buildToolsVersion '19.0.3'

compileSdkVersion project.ANDROID_COMPILE_SDK_VERSION
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 8
targetSdkVersion 19
minSdkVersion project.ANDROID_MIN_SDK_VERSION
targetSdkVersion project.ANDROID_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
versionName "1.0.0"
}

buildTypes {
release {
runProguard false
Expand All @@ -20,6 +22,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile project(':lib')
compile 'com.android.support:appcompat-v7:19.+'
}
3 changes: 1 addition & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ms.square.android.mymodule.app.MainActivity"
android:label="@string/app_name" >
android:name="com.ms.square.android.expandabletextview.sample.DemoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Loading

0 comments on commit 598fd2f

Please sign in to comment.