I studied how to make a floating action menu from floating action buttons (FAB), and I came up with this question . In the comments, this comment on it was given by a class that offers the FloatingActionMenu parameter. I have implemented a class file and xml and it only works with one problem.
Problem: There are four FABs in the menu, the first is a button that you click to expand the menu. I would like to have a distance between all the FABs, but there is no interval between them, and I cannot add any. (See image below)

Class FloatingActionMenu
public class FloatingActionMenu extends ViewGroup { private static final long ANIMATION_DURATION = 300; private FloatingActionButton mMenuButton; private ArrayList<FloatingActionButton> mMenuItems; private ArrayList<TextView> mMenuItemLabels; private ArrayList<ItemAnimator> mMenuItemAnimators; private int mItemMargin; private AnimatorSet mOpenAnimatorSet = new AnimatorSet(); private AnimatorSet mCloseAnimatorSet = new AnimatorSet(); private static final int DEFAULT_CHILD_GRAVITY = Gravity.END | Gravity.BOTTOM; private ImageView mIcon; private boolean mOpen; private boolean animating; private boolean mIsSetClosedOnTouchOutside = true; public interface OnMenuToggleListener { void onMenuToggle(boolean opened); } public interface OnMenuItemClickListener { void onMenuItemClick(FloatingActionMenu fam, int index, FloatingActionButton item); } private OnMenuItemClickListener onMenuItemClickListener; private OnMenuToggleListener onMenuToggleListener; public FloatingActionMenu(Context context) { this(context, null, 0); } public FloatingActionMenu(Context context, AttributeSet attrs) { this(context, attrs, 0); } public FloatingActionMenu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mMenuItems = new ArrayList<>(5); mMenuItemAnimators = new ArrayList<>(5); mMenuItemLabels = new ArrayList<>(5); mIcon = new ImageView(context); } @Override protected void onFinishInflate() { bringChildToFront(mMenuButton); bringChildToFront(mIcon); super.onFinishInflate(); } @Override public void addView(@NonNull View child, int index, LayoutParams params) { super.addView(child, index, params); if (getChildCount() > 1) { if (child instanceof FloatingActionButton) { addMenuItem((FloatingActionButton) child); } } else { mMenuButton = (FloatingActionButton) child; mIcon.setImageDrawable(mMenuButton.getDrawable()); addView(mIcon); mMenuButton.setImageDrawable(null); createDefaultIconAnimation(); mMenuButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { toggle(); } }); } } public void toggle() { if (!mOpen) { open(); } else { close(); } } public void open() { d("open"); startOpenAnimator(); mOpen = true; if (onMenuToggleListener != null) { onMenuToggleListener.onMenuToggle(true); } } public void close() { startCloseAnimator(); mOpen = false; if (onMenuToggleListener != null) { onMenuToggleListener.onMenuToggle(true); } } protected void startCloseAnimator() { mCloseAnimatorSet.start(); for (ItemAnimator anim : mMenuItemAnimators) { anim.startCloseAnimator(); } } protected void startOpenAnimator() { mOpenAnimatorSet.start(); for (ItemAnimator anim : mMenuItemAnimators) { anim.startOpenAnimator(); } } public void addMenuItem(FloatingActionButton item) { mMenuItems.add(item); mMenuItemAnimators.add(new ItemAnimator(item)); TextView button = new TextView(getContext()); button.setBackgroundResource(R.drawable.rounded_corners); button.setPadding(8,8,8,8); button.setTextColor(Color.WHITE); button.setText(item.getContentDescription()); addView(button); mMenuItemLabels.add(button); item.setTag(button); item.setOnClickListener(mOnItemClickListener); button.setOnClickListener(mOnItemClickListener); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int width; int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int height; final int count = getChildCount(); int maxChildWidth = 0; for (int i = 0; i < count; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); } for (int i = 0; i < mMenuItems.size(); i++) { FloatingActionButton fab = mMenuItems.get(i); TextView label = mMenuItemLabels.get(i); maxChildWidth = Math.max(maxChildWidth, label.getMeasuredWidth() + fab.getMeasuredWidth() + mItemMargin); } maxChildWidth = Math.max(mMenuButton.getMeasuredWidth(), maxChildWidth); if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { width = maxChildWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else { int heightSum = 0; for (int i = 0; i < count; i++) { View child = getChildAt(i); heightSum += child.getMeasuredHeight(); } height = heightSum; } setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec)); }
XML menu:
<terranovaproductions.newcomicreader.FloatingActionMenu android:id="@+id/fab_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/search" android:paddingBottom="@dimen/menu_button_margin" fab:fabSize="normal" android:id="@+id/fab_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_random" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/default_random" android:src="@drawable/random" android:paddingBottom="@dimen/menu_button_margin" fab:fabSize="mini"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/default_search" android:src="@drawable/search" android:paddingBottom="@dimen/menu_button_margin" fab:fabSize="mini"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab_browser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/default_browser" android:src="@drawable/browseropen" android:paddingBottom="@dimen/menu_button_margin" fab:fabSize="mini"/> </terranovaproductions.newcomicreader.FloatingActionMenu>
This XML is in a RelativeLayout with other elements.
What I tried:
- I tried adding an add-on in XML to individual buttons
- I tried to add fields to individual buttons in both the class and XML
- I searched where the buttons are drawn to find a way to add a place, but I had no luck.