Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support api 8 and up #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ ext {
compileSdkVersion = 21
buildToolsVersion = "21.1.2"

minSdkVersion = 14
minSdkVersion = 8
targetSdkVersion = 21
}
33 changes: 25 additions & 8 deletions library/src/main/java/me/grantland/widget/AutofitHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason you moved this to an anonymous class? seems to have the same functionality as before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a lazy initialization, there is no need for this instance in older versions...

@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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this will only work for AutofitTextView instances and not any other TextView instance. Is this correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, I've done this a while back so it's hard to rember exactly, I only needed it for AutofitTextView

}

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);
}
Expand Down Expand Up @@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use hungarian notation to match the style in the rest of the file

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: also keep this up top with other ivars

private class AutofitSizeChangedListener implements AutofitTextView.AutofitSizeChangedListener {
public void onChange(){
autofit();
}
}
Expand Down
28 changes: 28 additions & 0 deletions library/src/main/java/me/grantland/widget/AutofitTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down