Reputation: 8021
I have a class that programmatically adds buttons to a layout, but I need the buttons that are added to be flush with the bottom of the layout and with eachother - no margins visible.
As you can see in the attached screenshot - there is a margin both below the buttons, between the buttons and to the left and right of the buttons. I need there to be no margins - the buttons should fill the entire lower part of the layout.
Here is my xml for the container layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/someclass_rootlayout"
android:orientation="vertical"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="somepackage.someclass"
>
<LinearLayout
android:id="@+id/someclass_buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
/>
</LinearLayout>
And here is my code for adding the buttons programatically in a class that receives as input an array (buttonNames) containing the String names of the buttons to be added. For testing purposes I'm always passing an array of two elements and positioning them to the left and to the right of the layout.
private Window window;
private WindowManager.LayoutParams windowParams;
private int resourceIdentifier = 0;
...
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.someclass_fragment, container, false);
ButterKnife.inject(this, view);
window = getDialog().getWindow();
windowParams = window.getAttributes();
window.requestFeature(Window.FEATURE_NO_TITLE);
someclass_buttonlayout.setWeightSum(buttonNames.size());
for (String string : buttonNames) {
addNewButton(string);
resourceIdentifier++;
}
return view;
}
private void addNewButton(String name) {
Button newbutton = new Button(context);
newbutton.setText(name);
newbutton.setId(resourceIdentifier);
newbutton.setTextColor(getResources().getColor(R.color.black));
newbutton.setMinHeight(0);
newbutton.setMinWidth(0);
newbutton.setShadowLayer(0,0,0,0);
newbutton.setBottom(0);
newbutton.setPadding(0,0,0,0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (resourceIdentifier==0) {
layoutParams.gravity = Gravity.LEFT;
} else {
layoutParams.gravity = Gravity.RIGHT;
}
layoutParams.weight = 1;
newbutton.setLayoutParams(layoutParams);
newbutton.setOnClickListener(this);
newbutton.setTag(name);
someclass_buttonlayout.addView(newbutton);
}
Does anyone have any ideas why despite everything these buttons still have a margin around them, and if there's any way of removing that margin?
Upvotes: 0
Views: 64
Reputation: 5083
You should add background color to the button or buttons while creating the buttons dynamically like this:
newbutton.setBackgroundColor(Color.parseColor("#848482")); //#848482 is dark gray
Upvotes: 2
Reputation: 2535
Set Background color or drawable to your button. That will remove the padding as well as Margins.
Upvotes: 0