Misaligned floatingactionbutton

Asked

Viewed 166 times

0

Hello

I have a Tablelayout created dynamically, and I want to insert a FAB on the screen. The problem is I can’t get the normal alignment in the lower right corner.

Am I missing a parameter? Follow 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.

Screenshot com o FAB desalinhado

2 answers

0

The error is simple in Coordinate Layout you have to use the Constraint set to align the views in this case 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 well in simple layout like Linear or Relative

0

I solved the problem by changing the Layoutparams from Coordinatorlayout to Tablelayout. But I still don’t understand why the first one didn’t work. I’d appreciate it if someone could explain.

The new code went 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;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.