odds0cks
odds0cks

Reputation: 141

Linear Layouts Overlapping

For some reason my LinearLayouts that I am adding to my RelativeLayout that is within a ScrollView are overlapping each other. I assume there is some way of setting the LinearLayouts so they would position themselves below the LinearLayout above?

 exampleLayout = (RelativeLayout)findViewById(R.id.examplelayout);


         exampleButton = new TextView[exampleListCount];

         LinearLayout.LayoutParams rowsLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         rowsLayoutParams.gravity = Gravity.CENTER;
         rowsLayoutParams.setMargins(10, 10, 10, 0);

         LinearLayout.LayoutParams exampleButtonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         exampleButtonParams.setMargins(10, 10, 10, 0);

         exampleRowLayoutArray = new LinearLayout[rowsNeededInt];

         int exampleAddedCount = 0;
         for(int x = 0; x < rowsNeededInt; x++){
             exampleRowLayoutArray[x] = new LinearLayout(this);
             exampleRowLayoutArray[x].setOrientation(LinearLayout.HORIZONTAL);
             exampleRowLayoutArray[x].setLayoutParams(rowsLayoutParams);

             for(int i =0; i < 6; i++){

                if(exampleAddedCount < examplerListCount){

                    String exampleNo = String.valueOf(exampleList.get(exampleAddedCount));

                    exampleButton[exampleAddedCount] = new TextView(this);
                    exampleButton[exampleAddedCount].setId(exampleAddedCount+1);
                    exampleButton[exampleAddedCount].setGravity(Gravity.CENTER);
                    exampleButton[exampleAddedCount].setText(exampleNo);
                    exampleButton[exampleAddedCount].setLayoutParams(exampleButtonParams);
                    exampleButton[exampleAddedCount].setTextColor(getResources().getColor(android.R.color.white));
                    exampleButton[exampleAddedCount].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
                    exampleButton[exampleAddedCount].setBackgroundDrawable(new BitmapDrawable(getResources(), ButtonNormal));

                    exampleRowLayoutArray[x].addView(exampleButton[exampleAddedCount]);
                    exampleAddedCount++;
                }

             }


             exampleLayout.addView(exampleRowLayoutArray[x]);

         }

Xml

<ScrollView 
        android:id="@+id/examplescrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_below="@+id/topbar">

    <RelativeLayout
        android:id="@+id/examplelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        </RelativeLayout>

   </ScrollView>

Upvotes: 0

Views: 571

Answers (1)

laalto
laalto

Reputation: 152917

You don't seem to need the RelativeLayout's relative layout properties for anything, so the simplest would be to replace it with a vertical LinearLayout. Or if you need a scrolling list of items, consider a ListView.

In a RelativeLayout, you can achieve the same effect as vertical LinearLayout with the following:

  • Generate an id for each child

  • Create RelativeLayout.LayoutParams with LAYOUT_BELOW rule referencing the previous child.

  • Assign the layout params to the child.

Upvotes: 2

Related Questions