Skip to content

Commit

Permalink
fixed crash on android 4.1 and padding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiz committed Jun 23, 2016
1 parent 5a03a50 commit 7bc02d9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

Expand Down Expand Up @@ -177,6 +178,12 @@ public float getCornerBottomRightRadius() {

@Override
public void setPadding(int left, int top, int right, int bottom) {
if (mBubbleImpl == null) {
Log.w("BubbleView", "mBubbleImpl == null on old Android platform");
setSuperPadding(left, top, right, bottom);
return;
}

mBubbleImpl.setPadding(left, top, right, bottom);
}

Expand Down
20 changes: 18 additions & 2 deletions library/src/main/java/com/cpiz/android/bubbleview/BubbleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -60,7 +61,6 @@ public void init(View view, Context context, AttributeSet attrs) {
mFillPadding = ta.getDimension(R.styleable.BubbleStyle_bb_fillPadding, 0);
mBorderColor = ta.getColor(R.styleable.BubbleStyle_bb_borderColor, Color.WHITE);
mBorderWidth = ta.getDimension(R.styleable.BubbleStyle_bb_borderWidth, 0);

ta.recycle();
}
updateDrawable(mParentView.getWidth(), mParentView.getHeight(), false);
Expand Down Expand Up @@ -266,6 +266,22 @@ public float getCornerBottomRightRadius() {
@SuppressWarnings("SuspiciousNameCombination")
@Override
public void setPadding(int left, int top, int right, int bottom) {
if (mHolderCallback == null) {
return;
}

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
StackTraceElement stack[] = (new Throwable()).getStackTrace();
for (int i = 0; i < 7; i++) {
if (stack[i].getClassName().equals(View.class.getName())
&& stack[i].getMethodName().equals("recomputePadding")) {
Log.w("BubbleImpl", "Called setPadding by View on old Android platform");
mHolderCallback.setSuperPadding(left, top, right, bottom);
return;
}
}
}

mPaddingLeftOffset = mPaddingTopOffset = mPaddingRightOffset = mPaddingBottomOffset = 0;
switch (mArrowDirection) {
case Left:
Expand Down Expand Up @@ -348,7 +364,7 @@ protected void updateDrawable(int width, int height, boolean drawImmediately) {
arrowToOffsetY = mRectTo.centerY() - mRectSelf.centerY();
mArrowDirection = getAutoArrowDirection(width, height, arrowToOffsetX, arrowToOffsetY, (int) mArrowHeight);
}
mParentView.setPadding(mParentView.getPaddingLeft(), mParentView.getPaddingTop(), mParentView.getPaddingRight(), mParentView.getPaddingBottom());
setPadding(mParentView.getPaddingLeft(), mParentView.getPaddingTop(), mParentView.getPaddingRight(), mParentView.getPaddingBottom());

if (drawImmediately) {
mBubbleDrawable.resetRect(width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

Expand Down Expand Up @@ -177,6 +178,12 @@ public float getCornerBottomRightRadius() {

@Override
public void setPadding(int left, int top, int right, int bottom) {
if (mBubbleImpl == null) {
Log.w("BubbleView", "mBubbleImpl == null on old Android platform");
setSuperPadding(left, top, right, bottom);
return;
}

mBubbleImpl.setPadding(left, top, right, bottom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

Expand Down Expand Up @@ -177,6 +178,12 @@ public float getCornerBottomRightRadius() {

@Override
public void setPadding(int left, int top, int right, int bottom) {
if (mBubbleImpl == null) {
Log.w("BubbleView", "mBubbleImpl == null on old Android platform");
setSuperPadding(left, top, right, bottom);
return;
}

mBubbleImpl.setPadding(left, top, right, bottom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

Expand Down Expand Up @@ -176,6 +177,12 @@ public float getCornerBottomRightRadius() {

@Override
public void setPadding(int left, int top, int right, int bottom) {
if (mBubbleImpl == null) {
Log.w("BubbleView", "mBubbleImpl == null on old Android platform");
setSuperPadding(left, top, right, bottom);
return;
}

mBubbleImpl.setPadding(left, top, right, bottom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.cpiz.android.bubbleview.BubbleTextView;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";

Expand All @@ -11,5 +13,10 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setContentView(R.layout.activity_test);

BubbleTextView bubbleTextView = (BubbleTextView) findViewById(R.id.bubble);
if (bubbleTextView != null) {
bubbleTextView.setPadding(50, 50, 50, 50);
}
}
}

0 comments on commit 7bc02d9

Please sign in to comment.