Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ dependencies {
api "androidx.appcompat:appcompat:$versions.androidx_appcompat"
api "com.google.android.material:material:$versions.android_material"
api "androidx.cardview:cardview:$versions.androidx_cardview"
api "androidx.constraintlayout:constraintlayout:$versions.androidx_constraintlayout"
annotationProcessor "com.uber.nullaway:nullaway:$versions.nullaway"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,34 @@
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.leinardi.android.speeddial.SpeedDialView.OnActionSelectedListener;

import java.lang.annotation.Retention;

import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_AUTO;
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_MINI;
import static com.google.android.material.floatingactionbutton.FloatingActionButton.SIZE_NORMAL;
import static com.leinardi.android.speeddial.SpeedDialActionItem.RESOURCE_NOT_SET;
import static java.lang.annotation.RetentionPolicy.SOURCE;

/**
* View that contains fab button and its label.
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class FabWithLabelView extends LinearLayout {
public class FabWithLabelView extends ConstraintLayout {
private static final String TAG = FabWithLabelView.class.getSimpleName();

private TextView mLabelTextView;
Expand All @@ -66,6 +69,15 @@ public class FabWithLabelView extends LinearLayout {
private float mLabelCardViewElevation;
@Nullable
private Drawable mLabelCardViewBackground;
@Orientation
private int mOrientation = Orientation.HORIZONTAL;

@Retention(SOURCE)
@IntDef({Orientation.HORIZONTAL, Orientation.VERTICAL})
public @interface Orientation {
int HORIZONTAL = 0;
int VERTICAL = 1;
}

public FabWithLabelView(Context context) {
super(context);
Expand All @@ -92,15 +104,10 @@ public void setVisibility(int visibility) {
}
}

@Override
public void setOrientation(int orientation) {
super.setOrientation(orientation);
public void setOrientation(@Orientation int orientation) {
mOrientation = orientation;
setFabSize(mCurrentFabSize);
if (orientation == VERTICAL) {
setLabelEnabled(false);
} else {
setLabel(mLabelTextView.getText().toString());
}
setLabel(mLabelTextView.getText().toString());
}

/**
Expand All @@ -113,9 +120,9 @@ public boolean isLabelEnabled() {
/**
* Enables or disables label of button.
*/
private void setLabelEnabled(boolean enabled) {
mIsLabelEnabled = enabled;
mLabelCardView.setVisibility(enabled ? View.VISIBLE : View.GONE);
public void setLabelEnabled(boolean enabled) {
mIsLabelEnabled = enabled && !TextUtils.isEmpty(mLabelTextView.getText());
mLabelCardView.setVisibility(mIsLabelEnabled ? View.VISIBLE : View.GONE);
}

/**
Expand Down Expand Up @@ -254,7 +261,6 @@ private void init(Context context, @Nullable AttributeSet attrs) {
mLabelCardView = rootView.findViewById(R.id.sd_label_container);

setFabSize(SIZE_MINI);
setOrientation(LinearLayout.HORIZONTAL);
setClipChildren(false);
setClipToPadding(false);

Expand Down Expand Up @@ -296,28 +302,59 @@ private void setFabSize(@FloatingActionButton.Size int fabSize) {
int fabSizePx = fabSize == SIZE_NORMAL ? normalFabSizePx : miniFabSizePx;
LayoutParams rootLayoutParams;
LayoutParams fabLayoutParams = (LayoutParams) mFab.getLayoutParams();
if (getOrientation() == HORIZONTAL) {
if (mOrientation == Orientation.HORIZONTAL) {
rootLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, fabSizePx);
rootLayoutParams.gravity = Gravity.END;

LayoutParams cardParams = getHorizontalLabelLayoutParams(mFab);
mLabelCardView.setLayoutParams(cardParams);
int cardViewId = mLabelCardView.getId();
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
fabLayoutParams.startToEnd = cardViewId;
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
fabLayoutParams.bottomToBottom = LayoutParams.PARENT_ID;
if (fabSize == SIZE_NORMAL) {
int excessMargin = (normalFabSizePx - miniFabSizePx) / 2;
fabLayoutParams.setMargins(fabSideMarginPx - excessMargin, 0, fabSideMarginPx - excessMargin, 0);
} else {
fabLayoutParams.setMargins(fabSideMarginPx, 0, fabSideMarginPx, 0);

}
} else {
rootLayoutParams = new LayoutParams(fabSizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
rootLayoutParams.gravity = Gravity.CENTER_VERTICAL;
fabLayoutParams.setMargins(0, 0, 0, 0);
rootLayoutParams = new LayoutParams(fabSizePx, ViewGroup.LayoutParams.WRAP_CONTENT);
int cardViewId = mLabelCardView.getId();
LayoutParams cardParams = getVerticalLabelLayoutParams(mFab);
mLabelCardView.setLayoutParams(cardParams);
fabLayoutParams.endToEnd = LayoutParams.PARENT_ID;
fabLayoutParams.startToStart = LayoutParams.PARENT_ID;
fabLayoutParams.topToTop = LayoutParams.PARENT_ID;
fabLayoutParams.bottomToTop = cardViewId;
}

setLayoutParams(rootLayoutParams);
mFab.setLayoutParams(fabLayoutParams);
mCurrentFabSize = fabSize;
}

private static LayoutParams getVerticalLabelLayoutParams(View mainFabView) {
int fabId = mainFabView.getId();
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.startToStart = fabId;
layoutParams.endToEnd = fabId;
layoutParams.topToBottom = fabId;
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
return layoutParams;
}

private static LayoutParams getHorizontalLabelLayoutParams(View mainFabView) {
int fabId = mainFabView.getId();
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.startToStart = LayoutParams.PARENT_ID;
layoutParams.endToStart = fabId;
layoutParams.topToTop = LayoutParams.PARENT_ID;
layoutParams.bottomToBottom = LayoutParams.PARENT_ID;
return layoutParams;
}

/**
* Sets fab drawable.
*
Expand All @@ -335,7 +372,7 @@ private void setFabIcon(@Nullable Drawable mDrawable) {
private void setLabel(@Nullable CharSequence sequence) {
if (!TextUtils.isEmpty(sequence)) {
mLabelTextView.setText(sequence);
setLabelEnabled(getOrientation() == HORIZONTAL);
setLabelEnabled(true);
} else {
setLabelEnabled(false);
}
Expand Down
Loading