markormesher / android-fab

Floating action button (FAB) for Android with speed-dial menu functionality
Apache License 2.0
211 stars 29 forks source link

Can FAB speed dial items be aligned to parent left? #15

Closed barbeau closed 6 years ago

barbeau commented 7 years ago

Thanks again for the great library!

We have a setting in our app "Left hand mode" that flips FABs to align the left side of the screen instead of the right (used by a small but appreciative audience :)).

I can't seem to find a way to programmatically flip the FAB speed dial menu items so they align to the left side of the screen instead of the right.

Here's what I have so far - I was able to flip the FAB itself - heres, the screenshot:

image

Here's the code:

...
boolean leftHandMode = true;  // This should flip all FABs to the left side of screen
if (mLayersFab != null) {
    // Update all the Layers FAB-contained layouts
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mLayersFab.getFabContainer()
            .getLayoutParams();
    updateLeftHandMode(mLayersFab.getFabContainer(), layoutParams, leftHandMode);

    layoutParams = (RelativeLayout.LayoutParams) mLayersFab
            .getLayoutParams();
    updateLeftHandMode(mLayersFab, layoutParams, leftHandMode);
    layoutParams = (RelativeLayout.LayoutParams) mLayersFab.getButton().getLayoutParams();
    updateLeftHandMode(mLayersFab.getButton(), layoutParams, leftHandMode);

    for (int i = 0; i < mLayersFab.getButton().getChildCount(); i++) {
        View v = mLayersFab.getChildAt(i);
        RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) v.getLayoutParams();
        updateLeftHandMode(v, p, leftHandMode);
    }
}
...
private void updateLeftHandMode(View v, RelativeLayout.LayoutParams layoutParams,
        boolean leftHandMode) {
    if (leftHandMode) {
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
        }
    } else {
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
        }
    }
    v.setLayoutParams(layoutParams);
}

Despite looping through the children views, I can't seem to get the expanded speed dial to align left - here's what it looks like expanded:

image

Ideally the placement of the bike icon and "bikeshare" text would be reversed when changed to the left margin (so the bike icon is against left side of screen), but I'd take just moving the existing label and icon to the left margin as-is.

Am I missing something, or is this just not possible with the current library?

A WIP PR with the above code is at https://github.com/OneBusAway/onebusaway-android/pull/806 if you want to take it for a spin yourself.

markormesher commented 7 years ago

Hi Sean - thanks for raising this issue. Unfortunately this isn't really possible with the currently library due to some short-sighted hard-coded values. However, I've put a few hours into the re-write today and this has been top of my priorities. The button can now be positioned in each corner and the speed-dial menu is position properly (screenshots in follow-up comment).

The re-write will be ready for release shortly, but you're more than welcome to download the tip of the total-re-write branch and give it a try - it's quite stable, just not documented or 100% feature-complete yet.

markormesher commented 7 years ago

screenshot_20171022-185149

screenshot_20171022-185201

screenshot_20171022-185208

screenshot_20171022-185221

barbeau commented 7 years ago

Thanks for working on this Mark! I've been slammed lately but this looks great, I will give it a try.

markormesher commented 7 years ago

v2.0.0 is now published and available from Bintray. The demo app has been updated and should be live on the Play Store shortly.

Give it a spin and let me know how it goes. There are a few breaking changes but nothing major, and the demo app code shows more or less every feature.

barbeau commented 7 years ago

Thanks @markormesher! Sorry I haven't replied sooner - been slammed lately. I'll see if I can test v2.0.0 w/ OneBusAway Android.

barbeau commented 6 years ago

Updating to v2 of the FAB library is currently blocked by https://github.com/markormesher/android-fab/issues/24 and https://github.com/markormesher/android-fab/issues/25.

Now unblocked, both above issues are resolved.

barbeau commented 6 years ago

@markormesher Thanks, setting the FAB position in v2 works great! Feel free to close this - here's what our "left hand mode" looks like now:

image

barbeau commented 6 years ago

Just realized I can close this myself :). Thanks again for the great library!