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!
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);