Ryan Gray
Ryan Gray

Reputation: 824

Android XML Layout with buttons

I thought I understood XML.

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/button1"
    android:layout_width="109dp"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:clickable="true"
    android:onClick="closelog"
    android:text="@string/close"
     />



 <Button
    android:id="@+id/sendLogButton"
    android:layout_width="109dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:clickable="true"
    android:onClick="sendLog"
    android:text="@string/sendLog" />

<ListView
    android:id="@+id/loglist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

My buttons are not in the correct place on my layouts. Can someone explain to me why my two buttons can't occupy the same horizontal row when they are both the children of the same relative layout. enter image description here

Upvotes: 0

Views: 116

Answers (2)

Nam Vu
Nam Vu

Reputation: 5725

Because of

android:layout_gravity="left"

and

android:layout_gravity="right"

You must wrap 2 button in linear layout or relative layout

Upvotes: 0

Nick
Nick

Reputation: 9373

You need to wrap the buttons in a relative layout and then use attributes like android:layout_toRightOf="@id/anid and android:layout_toLeftOf="@id/anid to align the buttons to your liking. You also need to delete the layout_gravity.

See this post for more details on align elements in XML.

Upvotes: 3

Related Questions