Aligning Button to the bottom of CardView

599 Views Asked by At

So I'm creating some CardViews inside a for loop and I want to add a Button to the bottom of each CardView.

I'm setting the LayoutParams of the CardView like this

RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    height
            );
cardview.setLayoutParams(params4);

and create a new set of params for the Button

RelativeLayout.LayoutParams params20 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );

Add the align to bottom rule like this and then set these params to the LayoutParams of the Button

params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
search.setLayoutParams(params20);

In the end I add the Button to the CardView

cardView.addView(search);

But doing it like this the Button always stays at the top. When logging the CardViews and the parent of the Button the parent is as expected the current CardView

Log with 2 CardViews:

2021-01-27 10:15:45.843 7764-7764/lenno.plugin.smartplaner E/deine: cardview: androidx.cardview.widget.CardView{d12f026 VFE...CL. ......I. 0,0-0,0 #1}........id: 1
2021-01-27 10:15:45.843 7764-7764/lenno.plugin.smartplaner E/deine1: parent: androidx.cardview.widget.CardView{d12f026 VFE...CL. ......I. 0,0-0,0 #1}
2021-01-27 10:15:45.855 7764-7764/lenno.plugin.smartplaner E/deine: cardview: androidx.cardview.widget.CardView{ee5dc0a VFE...CL. ......I. 0,0-0,0 #2}........id: 2
2021-01-27 10:15:45.856 7764-7764/lenno.plugin.smartplaner E/deine1: parent: androidx.cardview.widget.CardView{ee5dc0a VFE...CL. ......I. 0,0-0,0 #2}

I already tried explicitly setting the anchor of params20 to the parent CardView like this

params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, cardView.getId());

which didn't work either. Maybe I'm doing something wrong in general (I just started creating Layouts programmatically) but I currently don't know what to do.

Thanks in advance for answers!

2

There are 2 best solutions below

0
AudioBubble On BEST ANSWER

I still don't know what the initial problem was but I fixed it by using a ConstraintLayout for the Button and setting the topMargin to a fitting value.

ConstraintLayout.LayoutParams params20 = new ConstraintLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params20.topMargin = 850;

search.setLayoutParams(params20);
cardView.addView(search);
4
Darkman On

Well here an example. Read the comments for info.

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        //CardView
        CardView cardview = new CardView(this);
        cardView.setId(1);
        RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1000);
        cardview.setLayoutParams(params4);

        //Button
        Button btn1 = new Button(this);
        btn1.setText("Button at the bottom");
        btn1.setId(2); //Only needed you wanna put another view close to this view. See below.
        RelativeLayout.LayoutParams params20 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params20.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1 /* ID of cardView */); //This will cause this view (button) to be positioned at the bottom of parent view.
        
        //Add the Button to the CardView. Make sure to include the layout params too.
        cardview.addView(btn1, params20);
        
        //If you wanna add another view above btn1, here's how.
        Button btn2 = new Button(this);
        btn2.setText("I'm above button one");
        RelativeLayout.LayoutParams params21 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params21.addRule(RelativeLayout.ABOVE, 2 /* ID of btn1 */);
        
        //Add btn2 to the CardView.
        cardview.addView(btn2, params21);
        
        //Main layout
        setContentView(cardview);
    }
}