1

I am trying to set the position of my RelativeLayout in the center of the screen. The RelativeLayout is inside of a ScrollView, with the following code. The catch is that in my emulator (Android 4.2) it works great, but if I execute it on a 2.3 version, the RelativeLayout ppears on the top left corner of the screen. I can´t figure out why this is happening.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="75dp"
    android:layout_marginLeft="50dp">
    <EditText
       android:id="@+id/when"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dip"
       android:layout_marginRight="10dip"
       android:layout_marginBottom="20dip"
       android:inputType="textMultiLine"
       android:scrollHorizontally="false"
       android:minLines="1"
       android:maxLines="3"
       android:imeOptions="actionDone|flagNoEnterAction" />  
   <EditText
       android:id="@+id/memory"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginRight="10dip"
       android:layout_marginTop="5dip"
       android:layout_below="@id/when"
       android:inputType="textMultiLine"
       android:scrollHorizontally="false"
       android:minLines="1"
       android:imeOptions="actionDone|flagNoEnterAction"
       android:hint="@string/add_memory_hint"
       android:background="#6656B3D4"  /> 
    <Button
        android:id="@+id/addMemory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/memory"
        android:layout_marginTop="20dp"
        android:padding="13dp"/>
    <Button
        android:id="@+id/neverMind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/addMemory"
        android:layout_marginTop="20dp"
        android:padding="13dp"/>


</RelativeLayout>

</ScrollView>

I would really appreciate some insight. Thank you.

Chayemor
  • 3,577
  • 4
  • 31
  • 54
  • Please post the complete xml file ... Scroll View beginning tag is missing in the above – Prem Dec 15 '13 at 22:53

3 Answers3

2

In your relative layout, Try adding the below tag. It should work.

android:layout_gravity="center"

Try adding background color to relative layout and scroll view and see how the change impacts. Only for debugging purpose.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/darker_gray" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="75dp"
    android:layout_marginLeft="50dp"

    android:background="@android:color/black">

I tried on device with OS 4.4.2

Prem
  • 4,823
  • 4
  • 31
  • 63
0

put it this way

scrollview
    linearLayout (gravity: center)
         YOUR relativelayour
         /YOUR relativelayour
    /linearLayout
/scrollview

although i don't see why you want to make something that is inside a scroll view in the center

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
0

Possibly the easiest would be to remove the margin from your RelativeLayout and wrap that in a LinearLayout with android:gravity="center" since RelativeLayout and ScrollView both don't have graviy, AFAIK. Something like this

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

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText
       android:id="@+id/when"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dip"
       android:layout_marginRight="10dip"
       android:layout_marginBottom="20dip"
       android:inputType="textMultiLine"
       android:scrollHorizontally="false"
       android:minLines="1"
       android:maxLines="3"
       android:imeOptions="actionDone|flagNoEnterAction" />  
   <EditText
       android:id="@+id/memory"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginRight="10dip"
       android:layout_marginTop="5dip"
       android:layout_below="@id/when"
       android:inputType="textMultiLine"
       android:scrollHorizontally="false"
       android:minLines="1"
       android:imeOptions="actionDone|flagNoEnterAction"
       android:hint="@string/add_memory_hint"
       android:background="#6656B3D4"  /> 
    <Button
        android:id="@+id/addMemory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/memory"
        android:layout_marginTop="20dp"
        android:padding="13dp"/>
    <Button
        android:id="@+id/neverMind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/addMemory"
        android:layout_marginTop="20dp"
        android:padding="13dp"/>


</RelativeLayout>
</LinearLayout>
</ScrollView>

I also removed orientation from the RelativeLayout since that ViewGroup doesn't have an orientation property.

codeMagic
  • 44,549
  • 13
  • 77
  • 93