Skip to content Skip to sidebar Skip to footer

Dynamically Adding A Button To Relativelayout Not Working Correctly

My goal is to add a number of buttons (in a 4 column grid) to a RelativeLayout depending on how many 'items' are in the database. When I was first learning how to add buttons to a

Solution 1:

The reason this is failing for you is because of the IDs you are using. Android uses "reserved" ids for things like the general content area of the app.

Using your code, I was able to add 1000 to each ID and generate the intended result.

Do note my cleanup below:

privatevoidloadItemButtons(){
    itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
    itemButtonLayout.removeAllViews();
    List<String> items = itemList;
    intcolCount=0;
    introwCount=0;

    // # of items per columnintcolSpan=4;

    finalintitemListSize= itemList.size();
    for (inti=0; i < itemListSize; i++) {
        intid=1000 + i;
        ItemButtonnewItemButton=newItemButton(this, items.get(i));
        RelativeLayout.LayoutParamslayoutParams=newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        newItemButton.setId(id);

        if(colCount == 0)
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        else
            layoutParams.addRule(RelativeLayout.RIGHT_OF, id-1);


        //If we are in any row except the top row, place in reference to the button above itif(rowCount != 0)
            layoutParams.addRule(RelativeLayout.BELOW, id-colSpan);

        newItemButton.setLayoutParams(layoutParams);
        itemButtonLayout.addView(newItemButton);

        if(colCount == colSpan - 1)
            rowCount += 1;

        colCount = (colCount + 1) % colSpan;
    }
}

Also, as mentioned in my comment on the original post, you really should use a gridview for this, it's what it was built for.

I can dig a hole with a pitchfork, but I bet a shovel would work better.

My Result

Solution 2:

I didn't test the code:

privatevoidloadItemButtons(){
    itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
    itemButtonLayout.removeAllViews();
    ArrayList<Item> items = db.getAllActiveItems();
    int colCount = 0;
    int rowCount = 0;
    int i = 0;

    while(i < items.size())
   {
        ItemButton newItemButton = new ItemButton(this, items.get(i));
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
        newItemButton.setId(i);

        if(colCount == 0)
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        else 
            layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);


        //If we are in any row except the top row, place in reference to the button above itif(rowCount != 0){
            layoutParams.addRule(RelativeLayout.BELOW, i-4);
        }

        itemButtonLayout.addView(newItemButton, i, layoutParams);

        if(colCount == 3){
            colCount = 0;
            rowCount += 1;
        }else{
            colCount += 1;
        }

        i++;
    }
}

Post a Comment for "Dynamically Adding A Button To Relativelayout Not Working Correctly"