0

So I want to add x amount of buttons to a layout in android (where x is usually between 4 and 24), and I can achieve this using the code below, but it doesn't scroll so it cuts off some of the buttons

I am using a fragment, which contains a LinearLayout and BottomNavigation, and one of those navigation options leads to the fragment contained below

At the moment my.xml file looks like:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/units_group"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

    </LinearLayout>

</ScrollView>

And my code:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_course_units, container, false);

    final LinearLayout unitsBtns = (LinearLayout) view.findViewById(R.id.units_group);

    //I actually get this from a volley request but didn't want to include all the code
    String[] units = {"1", "2", "3", "4", "5", "6", "7", "8"}; 

    for(int i = 0; i < units.length; i++){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );

        params.setMargins(0, 30, 0, 0);
        Button b = new Button(getActivity());

        b.setLayoutParams(params);
        b.setText(units[i]);
        b.setId(i);
        b.setBackgroundColor(Color.WHITE);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    // STUFF HERE
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        unitsBtns.addView(b);
    }

    return view;
}

Is there any way to get the buttons to be scrollable? I have tried to put the scrollView around the parent of this too and it still doesn't seem to work.

Note: I am still pretty new to developing native Android apps so please correct me if I'm doing something incorrectly

3
  • Use NestedScrollView instead of ScrollView Commented Sep 23, 2017 at 10:19
  • See my post it will help you.. Commented Sep 23, 2017 at 10:20
  • I dont know if this is going to help, but you can use a listView or recyclerview containing buttons. Both are scrollable. Commented Sep 23, 2017 at 10:50

2 Answers 2

2

when you have to use ScrollView you should give android:layout_height="wrap_content" in child view of ScrollView then only you have to scroll your view

You should use user LinearLayout like this

<LinearLayout
        android:id="@+id/units_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

    </LinearLayout>

here also you have to replace

  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

Note: when you give match_parent in height attribute, it will limit your view to device screen

hope it will help you.. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this! Turned out it was a mix of this and me forgetting the offset from the BottomNavigationView so it was hiding some of the bottom buttons
0

Here's a working example (horizontal) :

<HorizontalScrollView
        android:id="@+id/hsv1"
        android:scrollbars="none"
        android:background="#0d47a1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/hsvLayout1"
            android:background="#0d47a1"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </LinearLayout>
</HorizontalScrollView>

Get ScrollView and Layout:

HorizontalScrollView hsv1 = (HorizontalScrollView) findViewById( R.id.hsv1 );
LinearLayout layout = (LinearLayout) hsv1.findViewById( R.id.hsvLayout1 );
layout.removeAllViews();

Create a button dynamically:

Button myButton = new Button (this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
lp.setMargins( 20, 0, 20, 0 );
myButton.setLayoutParams(lp);
myButton.setTextColor( Color.YELLOW );
myButton.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
myButton.setText( "Hello World !" );

Add button to layout:

layout.addView(myButton);

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.