Search code examples
androidbuttonandroid-relativelayout

How to set multiple buttons in Relative Layout in multiple lines


I had created a layout programatically and created multiple buttons in it dynamically. I want when screen ends the remaining buttons show below of above buttons. Please do me the favour.

Any help is appreciated. Thanks in advance.


Solution

  • If you are creating the root layout dynamically you have to set root layout's layoutParams dynamically.

    LinearLayout ll = new LinearLayout(mContext);
                 ll.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    ll.setLayoutParams(layoutParams);
    

    and add the views to this layout

    Button btn=new Button(this);
    ll.addView(btn, layoutParams);
    

    didn't tested it please try this code

    Update

     RelativeLayout lLRoot = (RelativeLayout) findViewById(R.id.container);
    
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
    
        lLRoot.addView(ll);
    
        Button btn=new Button(this);
        btn.setText("hai");
        ll.addView(btn);