5

By default, views in a vertical LinearLayout are measured and layouts from top to bottom, just as on horizontal one from left to right.

Usually, if I want to have a layout that has its children measured from bottom to top, I usually use RelativeLayout, with an id for each of the children, while the bottom view has layout_alignParentBottom set to true, and the rest have "layout_above" set to the view below them:

<RelativeLayout>
 <View android:id="@+id/bottomView android:layout_alignParentBottom="true" />

 <View android:id="@+id/secondView android:layout_above="@+id/bottomView " />

 <View android:id="@+id/thirdView android:layout_above="@+id/secondView " />

 ...
</RelativeLayout

But this is annoying to set, and can have issues if being added into another layout.

Is it possible to have this behavior on LinearLayout? The only thing I've found is LayoutDirection, but this seems to belong to RTL direction (horizontal), so it doesn't work.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Why the downvote? I can't see this question being asked anywhere. – android developer Mar 12 '17 at 09:53
  • Have you tried adding the views to the linearlayout programmatically? If you do it, you can specify the position where they are placed – Javier Ventajas Hernández Mar 12 '17 at 10:24
  • Consider relying on LinearLayoutManager, then simply set the reverse value to true http://stackoverflow.com/questions/27727354/linearlayoutmanager-setreverselayout-true-but-items-stack-from-bottom – Khaledonia Mar 12 '17 at 11:06
  • @JavierVentajasHernández How would that help? They will still be put from top to bottom, so that if I remove the top one, all the others will take its place as the content will move up. In what I want, when I remove a view at the bottom, all views will move down to fill its space. – android developer Mar 12 '17 at 11:15
  • @Alexander It is possible, but makes it hard to manage compared to XML. – android developer Mar 12 '17 at 11:16
  • @androiddeveloper how so? it's a single line of code, you switch it on and off to your suit. It's quiet practical in my application. – Khaledonia Mar 12 '17 at 11:46
  • @Alexander It's not a single line of code for the views themselves, and the setting of the RecyclerView. The only single line is the creation of the LinearLayoutManager. – android developer Mar 12 '17 at 15:52

2 Answers2

4

Just set android:gravity="bottom" for your LinearLayout.

Okas
  • 2,664
  • 1
  • 19
  • 26
0

If you want to get child elements from bottom to top you can use Linear layout like this:

  LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
int i=count;
while(i!=0) {
    v = layout.getChildAt(i);
    //do something with your child element
    i--;
}
Rakshith Kumar
  • 872
  • 8
  • 20