FloatingActionButton misaligned

0

Hello

I have a dynamically created TableLayout, and I want to insert an FAB on the screen. The problem is that I can not get the normal alignment in the lower right corner.

Am I forgetting any parameters? Here is the code I'm using:

private FloatingActionButton getFAB() {
        
        FloatingActionButton fab = new FloatingActionButton(this);
        CoordinatorLayout.LayoutParams p =  new CoordinatorLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        p.setAnchorId(R.id.viewpagerOpcao);
        p.anchorGravity = Gravity.BOTTOM | Gravity.END | Gravity.RIGHT;
        fab.setLayoutParams(p);
        fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.design_fab_background));
        fab.setClipToOutline(true);
        fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.colorAccent)));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent criar = new Intent(MainActivity.this, MainOpcao.class);
                startActivity(criar);
            }
        });

To call this method, I do "table.addView (getFAB ());" where table is a TableLayout.

    
asked by anonymous 03.03.2016 / 00:23

2 answers

0

I solved the problem by switching the LayoutParams from CoordinatorLayout to TableLayout. But I still do not understand why the first one did not work. Thanks if anyone can explain.

The new code looks like this:

private FloatingActionButton getFAB() {
        FloatingActionButton fab = new FloatingActionButton(this);
        TableLayout.LayoutParams p = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        p.gravity =  Gravity.BOTTOM | Gravity.RIGHT | Gravity.END;
        p.setMarginEnd(16);
        fab.setLayoutParams(p);
        fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.design_fab_background));
        fab.setClipToOutline(true);
        fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.colorPrimaryDark)));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent criar = new Intent(MainActivity.this, MainOpcao.class);
                startActivity(criar);
            }
        });
        return fab;
    }
    
03.03.2016 / 16:02
0

The error is simple in Coordenator Layout you have to use the constraint set to align the views in this case it would look like this:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);

constraintSet.connect(button.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.RIGHT, constraintLayout.getId());
constraintSet.applyTo(constraintLayout);

The gravity method works fine in simple layout like Linear or Relative

    
12.09.2018 / 14:49