diff --git a/build.gradle b/build.gradle index 0a66527..e5be1df 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,6 @@ ext { compileSdkVersion = 21 buildToolsVersion = "21.1.2" - minSdkVersion = 14 + minSdkVersion = 8 targetSdkVersion = 21 } diff --git a/library/src/main/java/me/grantland/widget/AutofitHelper.java b/library/src/main/java/me/grantland/widget/AutofitHelper.java index 90e0b0e..229d153 100644 --- a/library/src/main/java/me/grantland/widget/AutofitHelper.java +++ b/library/src/main/java/me/grantland/widget/AutofitHelper.java @@ -239,8 +239,7 @@ else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { private TextWatcher mTextWatcher = new AutofitTextWatcher(); - private View.OnLayoutChangeListener mOnLayoutChangeListener = - new AutofitOnLayoutChangeListener(); + private View.OnLayoutChangeListener mOnLayoutChangeListener; private AutofitHelper(TextView view) { final Context context = view.getContext(); @@ -433,12 +432,31 @@ public AutofitHelper setEnabled(boolean enabled) { if (enabled) { mTextView.addTextChangedListener(mTextWatcher); - mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener); + + if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + if (mOnLayoutChangeListener == null){ + mOnLayoutChangeListener = 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) { + autofit(); + } + }; + } + mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener); + } else { + if ( mTextView instanceof AutofitTextView) + ((AutofitTextView) mTextView).setSizeListener(sizeListener); + } autofit(); } else { mTextView.removeTextChangedListener(mTextWatcher); - mTextView.removeOnLayoutChangeListener(mOnLayoutChangeListener); + if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mTextView.removeOnLayoutChangeListener(mOnLayoutChangeListener); + } else { + if ( mTextView instanceof AutofitTextView) + ((AutofitTextView) mTextView).setSizeListener(null); + } mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); } @@ -532,10 +550,9 @@ public void afterTextChanged(Editable editable) { } } - private class AutofitOnLayoutChangeListener implements View.OnLayoutChangeListener { - @Override - public void onLayoutChange(View view, int left, int top, int right, int bottom, - int oldLeft, int oldTop, int oldRight, int oldBottom) { + private AutofitSizeChangedListener sizeListener = new AutofitSizeChangedListener(); + private class AutofitSizeChangedListener implements AutofitTextView.AutofitSizeChangedListener { + public void onChange(){ autofit(); } } diff --git a/library/src/main/java/me/grantland/widget/AutofitTextView.java b/library/src/main/java/me/grantland/widget/AutofitTextView.java index f5cd9ba..ba9fd70 100644 --- a/library/src/main/java/me/grantland/widget/AutofitTextView.java +++ b/library/src/main/java/me/grantland/widget/AutofitTextView.java @@ -93,6 +93,34 @@ public void setSizeToFit() { setSizeToFit(true); } + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + sizeListener.onChange(); + } + + + public void setSizeListener(AutofitSizeChangedListener sizeListener) { + if ( sizeListener == null) + sizeListener = AutofitTextView.AutofitSizeChangedListener.EmptyAutofitSizeChangedListener.getInstance(); + this.sizeListener = sizeListener; + } + + private AutofitSizeChangedListener sizeListener = AutofitSizeChangedListener.EmptyAutofitSizeChangedListener.getInstance(); + public interface AutofitSizeChangedListener { + public void onChange(); + + public static class EmptyAutofitSizeChangedListener implements AutofitSizeChangedListener { + + @Override + public void onChange() {} + + private EmptyAutofitSizeChangedListener(){} + + private static EmptyAutofitSizeChangedListener instance = new EmptyAutofitSizeChangedListener(); + public static EmptyAutofitSizeChangedListener getInstance() { return instance;} + } + } /** * If true, the text will automatically be re-sized to fit its constraints; if false, it will * act like a normal TextView.