public void setPosition(int position, ViewGroup.LayoutParams layoutParams) {

        boolean setDefaultMargin = false;

        int gravity;
        switch (position) {
            case POSITION_TOP_CENTER:
                gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
                break;
            case POSITION_TOP_RIGHT:
                gravity = Gravity.TOP | Gravity.RIGHT;
                break;
            case POSITION_RIGHT_CENTER:
                gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
                break;
            case POSITION_BOTTOM_CENTER:
                gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
                break;
            case POSITION_BOTTOM_LEFT:
                gravity = Gravity.BOTTOM | Gravity.LEFT;
                break;
            case POSITION_LEFT_CENTER:
                gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
                break;
            case POSITION_TOP_LEFT:
                gravity = Gravity.TOP | Gravity.LEFT;
                break;
            case POSITION_BOTTOM_RIGHT:
            default:
                setDefaultMargin = true;
                gravity = Gravity.BOTTOM | Gravity.RIGHT;
                break;
        }
        if(!systemOverlay) {
            try {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParams;
                lp.gravity = gravity;
                setLayoutParams(lp);
            } catch (ClassCastException e) {
                throw new ClassCastException("layoutParams must be an instance of " +
                        "FrameLayout.LayoutParams, since this FAB is not a systemOverlay");
            }
        }
        else {
            try {
                WindowManager.LayoutParams lp = (WindowManager.LayoutParams) layoutParams;
                lp.gravity = gravity;
                if(setDefaultMargin) {
                    int margin =  getContext().getResources().getDimensionPixelSize(R.dimen.action_button_margin);
                    lp.x = margin;
                    lp.y = margin;
                }
                setLayoutParams(lp);
            } catch(ClassCastException e) {
                throw new ClassCastException("layoutParams must be an instance of " +
                        "WindowManager.LayoutParams, since this FAB is a systemOverlay");
            }
        }
    }
public void setPosition(int position, ViewGroup.LayoutParams layoutParams) {

        boolean setDefaultMargin = false;

        int gravity;
        switch (position) {
            case POSITION_TOP_CENTER:
                gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
                break;
            case POSITION_TOP_RIGHT:
                gravity = Gravity.TOP | Gravity.RIGHT;
                break;
            case POSITION_RIGHT_CENTER:
                gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
                break;
            case POSITION_BOTTOM_CENTER:
                gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
                break;
            case POSITION_BOTTOM_LEFT:
                gravity = Gravity.BOTTOM | Gravity.LEFT;
                break;
            case POSITION_LEFT_CENTER:
                gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
                break;
            case POSITION_TOP_LEFT:
                gravity = Gravity.TOP | Gravity.LEFT;
                break;
            case POSITION_BOTTOM_RIGHT:
            default:
                setDefaultMargin = true;
                gravity = Gravity.BOTTOM | Gravity.RIGHT;
                break;
        }
        if(!systemOverlay) {
            try {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParams;
                lp.gravity = gravity;
                setLayoutParams(lp);
            } catch (ClassCastException e) {
                throw new ClassCastException("layoutParams must be an instance of " +
                        "FrameLayout.LayoutParams, since this FAB is not a systemOverlay");
            }
        }
        else {
            try {
                WindowManager.LayoutParams lp = (WindowManager.LayoutParams) layoutParams;
                lp.gravity = gravity;
                if(setDefaultMargin) {
                    int margin =  getContext().getResources().getDimensionPixelSize(R.dimen.action_button_margin);
                    lp.x = margin;
                    lp.y = margin;
                }
                setLayoutParams(lp);
            } catch(ClassCastException e) {
                throw new ClassCastException("layoutParams must be an instance of " +
                        "WindowManager.LayoutParams, since this FAB is a systemOverlay");
            }
        }
    }
public void setPosition ( int position , ViewGroup . LayoutParams layoutParams ) { boolean setDefaultMargin = false ; int gravity ; switch ( position ) { case POSITION_TOP_CENTER : gravity = Gravity . TOP | Gravity . CENTER_HORIZONTAL ; break ; case POSITION_TOP_RIGHT : gravity = Gravity . TOP | Gravity . RIGHT ; break ; case POSITION_RIGHT_CENTER : gravity = Gravity . RIGHT | Gravity . CENTER_VERTICAL ; break ; case POSITION_BOTTOM_CENTER : gravity = Gravity . BOTTOM | Gravity . CENTER_HORIZONTAL ; break ; case POSITION_BOTTOM_LEFT : gravity = Gravity . BOTTOM | Gravity . LEFT ; break ; case POSITION_LEFT_CENTER : gravity = Gravity . LEFT | Gravity . CENTER_VERTICAL ; break ; case POSITION_TOP_LEFT : gravity = Gravity . TOP | Gravity . LEFT ; break ; case POSITION_BOTTOM_RIGHT : default : setDefaultMargin = true ; gravity = Gravity . BOTTOM | Gravity . RIGHT ; break ; } if ( ! systemOverlay ) { try { FrameLayout . LayoutParams lp = ( FrameLayout . LayoutParams ) layoutParams ; lp . gravity = gravity ; setLayoutParams ( lp ) ; } catch ( ClassCastException e ) { throw new ClassCastException ( "layoutParams must be an instance of " + "FrameLayout.LayoutParams, since this FAB is not a systemOverlay" ) ; } } else { try { WindowManager . LayoutParams lp = ( WindowManager . LayoutParams ) layoutParams ; lp . gravity = gravity ; if ( setDefaultMargin ) { int margin = getContext ( ) . getResources ( ) . getDimensionPixelSize ( R . dimen . action_button_margin ) ; lp . x = margin ; lp . y = margin ; } setLayoutParams ( lp ) ; } catch ( ClassCastException e ) { throw new ClassCastException ( "layoutParams must be an instance of " + "WindowManager.LayoutParams, since this FAB is a systemOverlay" ) ; } } }
Sets the position of the button by calculating its Gravity from the position parameter
@param position one of 8 specified positions.
@param layoutParams should be either FrameLayout.LayoutParams or WindowManager.LayoutParams
Sets the position of the button by calculating its Gravity from the position parameter