Reputation: 2069
I am dynamically adding views to a linear layout. I want the view to be in reverse order. Like first I added RadioButton and then I added CheckBox so I want CheckBox on top then RadioButton. In simple newly added view should be shown on top. I'm not using Recycelerview nor I can.
I have tried android:layoutDirection
but it only working from RTL or LTR.
This is how I'm adding them in linear layout
CardView cardView = new CardView(this);
LinearLayout.LayoutParams cardParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
cardParams.setMargins(10, 10, 10, 10);
cardView.setLayoutParams(cardParams);
LinearLayout cardLayout = new LinearLayout(this);
cardLayout.setOrientation(LinearLayout.VERTICAL);
cardView.addView(cardLayout);
linearLayout.addView(cardView);
final EditText questionText = new EditText(this);
LinearLayout.LayoutParams textParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
textParams.setMargins(20, 10, 20, 10);
questionText.setPadding(15, 10, 10, 10);
questionText.setText(title);
edtTags.add(questionText);
questionText.setTag(edtTags.size());
questionText.setLayoutParams(textParams);
cardLayout.addView(questionText);
Upvotes: 1
Views: 439
Reputation: 896
As per the documentation, you can specify an index as the second parameter, so the following would do what you want
linearLayout.addView(cardView, 0);
Upvotes: 1