black
black

Reputation: 151

How to add dynamic buttons vertically one after another in android?

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

Upvotes: 0

Views: 2156

Answers (3)

Rashid
Rashid

Reputation: 1750

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
}

Upvotes: 1

saber safavi
saber safavi

Reputation: 452

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);

Upvotes: 0

MathankumarK
MathankumarK

Reputation: 2905

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);

Upvotes: 0

Related Questions