0

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.

screenshot of the problem

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?

4
  • @PsyDuck Thanks, but actually they have weight. The weightsum of the buttonlayout is equal to the size of the array passed to the class (number of buttons to be added) and each button is given a weight of 1. Commented Dec 30, 2014 at 10:57
  • @PsyDuck just the default Button that android provides - no manipulation of the background Commented Dec 30, 2014 at 11:06
  • @PsyDuck however, now that you mention it.. I tried changing the color of the button with setBackgroundColor and suddenly the margins vanished. They must have been some sort of 3-dimensionalization on the buttons and not a real margin at all :/ Commented Dec 30, 2014 at 11:07
  • @PsyDuck It works - please post an answer so I can give you credit for this. Commented Dec 30, 2014 at 11:31

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

Set Background color or drawable to your button. That will remove the padding as well as Margins.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.