Android vertical center

Go To StackoverFlow.com

1

Why aren't these centered vertically? I am struggling with this and cannot make it work..

This is in a relativelayout and on the bottom of the display.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="46dp"
style="?headerfooter"
android:id="@+id/linlay2"
android:layout_below="@+id/ListView01"
android:gravity="center_vertical"
android:layout_alignBottom="@+id/mainrel">
<EditText 
    android:id="@+id/EditText_AddNewList" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:hint="@string/dialog_addnew_lists_listnamehint"
    android:layout_centerVertical="true"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:layout_weight="1">
</EditText>
<Button 
        android:id="@+id/bSQLUpdate" 
        android:layout_width="40dp" 
        android:layout_height="40dp" 
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        style="?button_add"/>
</LinearLayout>

enter image description here

2012-04-03 20:35
by erdomester
Not sure if this helps but I find that when things aren't laying out properly on Android setting the weight attribute to 1 sometimes helps. android:layout_weight="1" . Try it on your button - PaulG 2012-04-03 20:39


0

Two suggestions:

1) What if you set the layout_height of your LinearLayout to wrap_content?
2) I suspect that your EditText may be slightly too big (I've run into similar issues before) so if you change its height from wrap_content to match_parent you might have better luck.

2012-04-03 20:51
by Jon O
>
  • No difference 2. match_parent is unavailable for me, I am using 2.1 API 7, 8 is needed
  • - erdomester 2012-04-03 21:01
    fill_parent and match_parent resolve to the same thing. match_parent is just a more accurate description. So, try fill_parent! : - Jon O 2012-04-03 21:05
    You can also try setting the layout_height (and potentially the layout_width too) on the EditText to 0dp. Basically, this will make it use the layout_weight of 1 that you've assigned it in order to fill the remaining space after all else has been laid out - Jon O 2012-04-03 21:09
    Setting the layout_height to 0dp makes it disappear. - erdomester 2012-04-03 21:50
    How very odd. It should scale up since its weight is 1. What about setting it to 40dp? How about 35dp? The idea is to see if it centers better if we shrink it vertically - Jon O 2012-04-03 21:52
    With 40dp its like its alignedTop with the button. with 35 it is lower but not in the center. Setting android:layout_marginTop="2dp" almost solves the problem. Maybe it has something to do with the parent relativelayout. I will try with LinearLayout later - erdomester 2012-04-03 22:04
    It's the same with linearlayout. I simply dont understand thi - erdomester 2012-04-05 17:52
    It turned out they are both centered vertically. It must be a pixel error sg that makes them look like not the way they should be aligne - erdomester 2012-04-05 18:07
    Come to think of it, I think I've run into that with EditTexts before. Maybe EditText is designed to have a "deep" or "hollow" look to it and therefore the white background is offset and a shadow applied at the top of it? That might explain the few-pixel offset - Jon O 2012-04-06 14:42


    0

    Have you tried using just "center" rather than "center_vertical"? Also, if you're looking to center things down an imaginary line running from the top of the screen to the bottom of the screen, center_horizontal would be what you're looking for.

    Here's some code that places an item dead in the middle of the screen:

    <?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:orientation="vertical" >
    
          <ProgressBar
            android:id="@+id/electionsProgressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" android:layout_gravity="center_vertical|center_horizontal"/> 
    
    </LinearLayout>
    
    2012-04-03 20:52
    by Davek804
    I know how to center. That is why I don't get why this is not workin - erdomester 2012-04-03 21:02
    Ads