1

I'm trying to implement an scrollable horizontal Linearlayout inside scrollview in a method dynamically , but the view (LinearLayout) does not scroll!

This method get some result from server and then create the view dynamically. "layout_services" is an vertical LinearLayout which is implemented inside xml file.

Voila the code :

 for (ServiceResult serviceResult : response.getResult()) {
        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textViewParams.setMargins(15, 15, 15, 15);
        textViewParams.gravity = Gravity.RIGHT;
        TextView textView = new TextView(getActivity());
        textView.setPadding(5, 5, 5, 5);
        textView.setText(serviceResult.getCategory());
        textView.setLayoutParams(textViewParams);
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.LTGRAY);
        if (!serviceResult.getServices().isEmpty()) {
            layout_services.addView(textView);
            LinearLayout.LayoutParams scrollLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.setLayoutParams(scrollLayoutParams);
            LinearLayout linearLayout = new LinearLayout(getActivity());
            linearLayout.setPadding(5, 5, 5, 5);
            LinearLayout.LayoutParams horizontalLayoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
            horizontalLayoutParams.gravity = Gravity.RIGHT;
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setLayoutParams(horizontalLayoutParams);
            scrollView.addView(linearLayout);
            for (int i = 0; i < serviceResult.getServices().size(); i++) {
                int finalI = i;
                Button btn = new Button(getActivity());
                btn.setText(serviceResult.getServices().get(finalI).getName());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 200);
                params.setMargins(15, 15, 15, 15);
                params.gravity = Gravity.LEFT;
                btn.setLayoutParams(params);
                btn.setTextColor(Color.BLACK);
                btn.setOnClickListener(view -> {
                });
                linearLayout.addView(btn);
            }
            layout_services.addView(scrollView);
        }
    }
0

2 Answers 2

4

Use HorizontalScrollView instead of ScrollView as parent of Linear Layout that you want to scroll horizontally

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

1 Comment

always use horizontall scroll view to scroll horizontally
1

You can try nested scroll view.

Read the documentation here : Nested Scroll View

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.