Hi Im Rez
Hi Im Rez

Reputation: 3

Add spacing between buttons in LinearLayout that's inside a HorizontalScrollView

I've attempted to change the X position of the buttons, it works but the buttons then end up getting caught off by the HorizontalScrollView. All I want to do is add spacing between the buttons.

Here is the code I'm using to display the buttons programmatically:

@TargetApi(21)
private void loadProfiles() {
    HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
    LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.scrollLinear);
    if(getFilesDir().listFiles().length != 0) {
        for(File file : getFilesDir().listFiles()) {
            ImageButton button = new ImageButton(this);
            button.setLayoutParams(new ActionBar.LayoutParams(150, 150));
            button.setY(35);
            button.setBackground(getResources().getDrawable(R.drawable.roundedbutton, getTheme()));
            layout.addView(button);
        }
    }
}

Here is the HorizontalScrollView:

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@drawable/bar"
    android:id="@+id/horizontalScrollView"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollLinear"></LinearLayout>
</HorizontalScrollView>

Here is a picture of what the buttons currently look like:

Picture

Upvotes: 0

Views: 548

Answers (1)

Andreas Evjenth
Andreas Evjenth

Reputation: 500

ImageButton button = new ImageButton(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 150);
        layoutParams.setMargins(10,0,0,10);
        button.setLayoutParams(layoutParams);
        button.setY(35);
        button.setBackground(getResources().getDrawable(R.drawable.roundedbutton, getTheme()));
        layout.addView(button);

This should work

Upvotes: 1

Related Questions