I have to dynamically build views and I have used GridLayout as the parent layout to populate 2 views in a row. The code works perfectly fine in the platforms below Nougat. But in nougat version, the views are not populated.
Here is the code section below
private void populateAnswer(List<Answer> answerList, LinearLayout parent) {
GridLayout gridLayout = new GridLayout(context);
gridLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
gridLayout.setAlignmentMode(GridLayout.ALIGN_MARGINS);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
gridLayout.setLayoutParams(layoutParams);
gridLayout.setColumnCount(2);
Answer temp;
RadioGroup radioGroup = new RadioGroup(context);
if (answerList != null) {
for (int i = 0; i < answerList.size(); i++) {
temp = answerList.get(i);
if (temp.getaType().equals(RADIO_BUTTON)) {
RadioButton radioButton = new RadioButton(context);
radioButton.setTextColor(getResources().getColor(R.color.colorPrimary));
radioButton.setHighlightColor(getResources().getColor(R.color.colorPrimary));
GridLayout.LayoutParams rbLayoutParams = new GridLayout.LayoutParams();
rbLayoutParams.setGravity(Gravity.CENTER);
rbLayoutParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
rbLayoutParams.width = GridLayout.LayoutParams.MATCH_PARENT;
radioButton.setLayoutParams(rbLayoutParams);
radioButton.setText(temp.getaDesc());
radioGroup.addView(radioButton);
} else if (temp.getaType().equals(CHECK_BOX)) {
CheckBox checkBox = new CheckBox(context);
checkBox.setTextColor(getResources().getColor(R.color.colorPrimary));
checkBox.setHighlightColor(getResources().getColor(R.color.colorPrimary));
checkBox.setText(temp.getaDesc());
gridLayout.addView(checkBox);
} else if (temp.getaType().equals(FREE_TEXT)) {
EditText editText = new EditText(context);
editText.setHint(temp.getaDesc());
editText.setHintTextColor(getResources().getColor(android.R.color.darker_gray));
gridLayout.addView(editText);
}
}
}
if (radioGroup.getChildCount() > 0) {
gridLayout.addView(radioGroup);
}
parent.addView(gridLayout);
}
I have attached the screenshots for different devices.