Android XML - how to get elements aligned left, right and right
I have this XML code that generates a button, text view and another button. How do I get the button to appear on the far left, the text image in the center, and the last button on the right?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Place">
</TextView>
<Button android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save">
</Button>
</LinearLayout>
a source to share
You need to use the weight property. Think about it, since android knows the percentage of width each element should give.
you need to set width to 0dip for all three elements and add weight property
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_width="0dip"
android:layout_weight="2" >
</Button>
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Place"
android:layout_width="0dip"
android:layout_weight="1" >
</TextView>
<Button android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_width="0dip"
android:layout_weight="2" >
</Button>
Play with the weight and you will see how it works :)
After that, you can use the gravity property to move the text center / left or right.
a source to share
I would recommend using RelativeLayout, it made things easier.
using RelativeLayout you will be able to set the text view either horizontally centered or parent or center.
after that, you can either attach these buttons directly to the far sides, aligning them with the parent sides, or anchor them directly to the sides of the textbox, add a little padding and maybe you get your distance.
a source to share
If you want to keep the LinearLayout, you need to do the following: Create a TableRow and then place all the elements (both buttons and text view). this will create one straight line with all the points neatly placed. This code worked for me, Sorry, I can't take a picture of it (New and not enough reputation points), but it worked for me, I hope it helps you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginRight="30dp"
android:layout_marginLeft="10dp">
</Button>
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Place"
android:layout_gravity="center_horizontal"
android:layout_marginRight="30dp">
</TextView>
<Button android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_gravity="right">
</Button>
</TableRow>
</LinearLayout>
a source to share