0

I need to set margin to dynamically created UI. I want to add margin to LinearLayout.

Below is my code

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            viewPager = (ViewPager) container;      
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            this.layoutInflater = inflater;
            scrollView = new ScrollView(getActivity());
            linearLayout = new LinearLayout(getActivity());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            layoutParams.setMargins(15, 15, 15, 15);
            scrollView.setLayoutParams(layoutParams);   //add this
            scrollView.addView(linearLayout, layoutParams);

    //adding few UI controllers dynamically by method call to here


            return scrollView;
        }

I tried many ways but nothing works. Currently, it is not adding space/margin as per given dimensions.

VVB
  • 7,363
  • 7
  • 49
  • 83

4 Answers4

0
if(layoutParams instanceof MarginLayoutParams)
{
    ((MarginLayoutParams) layoutParams).topMargin = 15;
    ((MarginLayoutParams) layoutParams).leftMargin = 15;
    //... etc
}
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • I think the answer from rafsanahm.. serves your need since my solution is just to set margin to elements which cant be directly set. Anyway, i guess you want your whole view (scrollview wraps linearlayout) with margin. – Emanuel Apr 22 '17 at 18:49
0

you need to call scrollView.setLayoutParams(layoutParams);

also set Layoutparams to your Scrollview and Inner LinearLayout

in your code :

 scrollView = new ScrollView(getActivity());
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
scrollView.setFillViewport(true);

linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView
        .getLayoutParams();
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams);   //add this
scrollView.addView(linearLayout, layoutParams);
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

If you want to add margins to LinearLayout, you need to create LayoutParams of the same type of the parent, so:

scrollView = new ScrollView(getContext());
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// I recommend you to set resolved size as margins, not pixels like you are doing here.        
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);

EDIT

Remember to add the ScrollView to your layout adding LayoutParams to it. For example in a Fragment (you are using getActivity() so i suppose you are in a Fragment):

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ScrollView scrollView = new ScrollView(getActivity());
    LinearLayout linearLayout = new LinearLayout(getActivity());
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    layoutParams.setMargins(15, 15, 15, 15);
    scrollView.addView(linearLayout, layoutParams);
    scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    return scrollView;
}
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
0

As per this SO answer:

scrollView.setFillViewport(true);

This forces the childview to stretch to the parent scrollview. Then you can set the margins that will add space after filling the scrollview.

Community
  • 1
  • 1
flopshot
  • 1,153
  • 1
  • 16
  • 23