-1

I have a vertical linear layout with a 2 views inside.

how can i center this view vertically? what's the most cost efficient

1) replace the linear layout with relative layout and use android:center_toparent= true (expensive as two layout pass on every render)

2) put two place holder views one as first and last child. eachi with height = 0 and weight = 1 so the left space is spread equally. can i make these dummy views as visibility = invisible? it saves some cost?

Edric
  • 24,639
  • 13
  • 81
  • 91
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Have you measured performance and determined that this efficiency will actually affect your app? This smells like premature optimization; just do whatever makes your code the most maintainable over the long term. – Ben P. Sep 28 '18 at 19:22
  • no i haven't. not sure how to do it with so small changes. and thought to understand the logic before implementing and testing both which can be not needed – Elad Benda Sep 28 '18 at 19:28

1 Answers1

0

Unless your activity is going to have a lot (say, hundreds or thousands) of these views, it is extremely unlikely that a single extra layout pass or two extra spacer views will make any noticeable difference. Personally, I wouldn't even bother measuring which of these strategies is fastest; I would just do whatever is standard on my team or whatever I'm used to doing.

Saving three nanoseconds doesn't help your users in any way. Writing code that you'll be able to come back to in six months and understand immediately will help you or your teammates immensely.

My gut reaction for the "best" way to do this (in terms of long-term maintainability) is to use the android:gravity attribute on your LinearLayout. No need for spacer views.

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

    <View
        android:layout_width="match_parent"
        android:layout_height="96dp"
        android:background="#caf"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="96dp"
        android:background="#fac"/>

</LinearLayout>

enter image description here

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • thanks. can you please explain? (1) the two views are told to be centered in their parent. meaning the virtual group of both of them will be centered? (2) if the parent is a vertiacl linear layout, that means it puts children top-left and ignore vertical centering. so `gravity='canter'` means just horizontal gravity? or else? – Elad Benda Sep 29 '18 at 13:47
  • LinearLayout only ignores the vertical component of its **child `layout_gravity`** attributes. It uses both the horizontal and vertical components of **its own `gravity`** attribute. In this case, since the views stretch from edge to edge horizontally, `center` and `center_vertical` are functionally identical, and both would give you a layout where the children are arranged in a vertical line, but the total group is centered within the LinearLayout. – Ben P. Oct 02 '18 at 21:21