I am creating buttons and adding them to a LinearLayout programmatically. However, I am struggling with changing the button sizes. In the code below, no matter what num I enter in deleteBtn.setWidth(num), and deleteBtn.setHeight(num), nothing changes.
private void populateHorizontalLayouts(CustomMessageEvent event) {
// Need to remove all views each time an user adds a number
// so that the same number is not rendered multiple times.
nameAndNumbersLayout.removeAllViews();
//displayedNamesAndNumbers.add(event.getCustomMessage());
for(int i =0; i < displayedNamesAndNumbers.size(); i++){
String displayedNumberAndName = displayedNamesAndNumbers.get(i);
LinearLayout horizontalLayout = new LinearLayout(view.getContext());
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setId(i);
Button deleteBtn = new Button(view.getContext());
deleteBtn.setId(i);
deleteBtn.setText("Delete");
deleteBtn.setWidth(5);
deleteBtn.setHeight(5);
TextView nameAndNumView = new TextView(view.getContext());
nameAndNumView.setId(i);
nameAndNumView.setText(displayedNumberAndName);
horizontalLayout.addView(deleteBtn);
horizontalLayout.addView(nameAndNumView);
nameAndNumbersLayout.addView(horizontalLayout);
}
}
What should I write so that the button size changes?
You should apply LayoutParams
of the LinearLayout
which contains your Button
.
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Try this one
deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With