0

I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.

     RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
     RelativeLayout.LayoutParams buttonParams =
            new RelativeLayout.LayoutParams(
                  RelativeLayout.LayoutParams.WRAP_CONTENT,
                   RelativeLayout.LayoutParams.WRAP_CONTENT);




    Button btn;
    List<Button> allEds = new ArrayList<Button>();
    for(int i=0;i<totalSID;i++){
        btn = new Button(FaqList.this);
        btn.setId(i);
        allEds.add(btn);
        if(i==0){}
        else
        buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());

        body.addView(btn,buttonParams);


    }

Adding the xml file. For reference you can go through it.

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:fillViewport="true"
   android:scrollbars="vertical"
    >

  <HorizontalScrollView
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:fillViewport="true"

    >

    <LinearLayout
          android:layout_marginTop="20dp"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginBottom="20dp"
        >



        <LinearLayout
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"

            android:orientation="horizontal"
            android:animateLayoutChanges="true">

           </LinearLayout>
           </LinearLayout>

           </HorizontalScrollView>
         </ScrollView>

      </LinearLayout

I am not sure where is the problem actually

2
  • you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this : RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow); Commented Feb 10, 2016 at 8:33
  • if i use linearlayout i cant add the rules later, and in linear the buttons just go side by side horizontally :/ Commented Feb 10, 2016 at 8:37

3 Answers 3

1

If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.

int iNumberOfButtons =  productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];

LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

for (int i = 0; i < iNumberOfButtons; i++) {
    ProductType productType = productTypeList.get(i);
    dynamicButtons[i] = new Button(getActivity());
    dynamicButtons[i].setText(productType.getTitleString());
    dynamicButtons[i].setId(i);
    dynamicButtons[i].setTextSize(15.0f);
    dynamicButtons[i].setOnClickListener(this);
    dynamicButtons[i].setLayoutParams(paramsButton);
    dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Sign up to request clarification or add additional context in comments.

2 Comments

if i use linear, buttons go side by side horizontal :/
change orientation of your LinearLayout container to vertical, here is the code -> android:orientation="vertical"
0

Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need

LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);

2 Comments

i tried , but the buttons go side by side horizontal
Add gravity for Layout parems. params.gravity = Gravity.Center; button.setLayoutParams(params);
0

you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :

RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);

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.