1

I just started android programming . I have been writing xml in RelativeLayout and find that gravity attribute isn't working . Every element in layout overlaps each other. I am aware that there may be better ways to do positioning but I am curious to know , what is it ,that I am not doing correct ? Please help me out

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TextView 
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/message"
                 android:gravity="start"
            />

           <Button  
              android:id="@+id/btnclose"
              android:textColor="#ffffff"
              android:background="#780956"
              android:textSize="18sp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="close"
              android:text="close"
              android:gravity="bottom"
        />
    <Button  
        android:id="@+id/btnclick"
        android:textColor="#ffffff"
        android:background="#123456"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="close"
        android:text="click"
        android:gravity="center"
        />
</RelativeLayout>
Manishika
  • 5,478
  • 2
  • 22
  • 28
meShakti
  • 273
  • 1
  • 11

3 Answers3

1

not just use gravity

there are two types of them gravity ,layout-gravity

one affects all the elements and other won't

try both of them..

P-RAD
  • 1,293
  • 2
  • 15
  • 36
0

As you are using RelativeLayout, right way to position views are using below attributes:

  1. android:centerInParent
  2. android:layout_toLeftOf
  3. android:layout_toRightOf
  4. android:layoutBelow

Read more about Positing Views.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

As you are using RelativeLayout, set properties this way:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="message" />

    <Button
        android:id="@+id/btnclose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#780956"
        android:onClick="close"
        android:text="close"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btnclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#123456"
        android:layout_centerInParent="true"
        android:onClick="close"
        android:text="click"
        android:textColor="#ffffff"
        android:textSize="18sp" />


</RelativeLayout>
Pang
  • 9,564
  • 146
  • 81
  • 122
Jignesh Jain
  • 1,518
  • 12
  • 28