I know this can be very easily done by using a Relative Layout but I feel what I am doing is correct and should give me the desired result with the Linear Layout itself. But for some reason, when I run on this on Google Nexus 7 running Android JB 4.1.2, I see the button and the text view immediately after the list view items. If the list view is empty, I see them at the top of the screen. Am I doing something wrong?? Here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/attachments_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:orientation="vertical">
<ListView
android:id="@+id/mtg_attachments"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/attach_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/delete"
android:textSize="22sp" />
<TextView
android:text="@string/attach_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and right for each. Save this answer.
Make the Parent of the LinearLayout containing the button as relative and set the property of linear layout containing two buttons as android:layout_alignParentBottom="true". You can also set the gravity of the Linear Layout to Bottom. Save this answer.
A simple problem but many new developers don't know how to do this. Set CSS Position property to fixed, bottom and right properties to 0px. Use cases : A feedback button or a chat window.
Defining RelativeLayout in layout XML This is done by using the android:layout_below="@+id/textView" attribute in both the Button tags. We have aligned both the buttons from the top margin (of each other), using android:layout_alignTop="@id/button" attribute.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/attachments_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="@+id/mtg_attachments"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_weight="0.9" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0.1"
android:gravity="bottom" >
<Button
android:id="@+id/attach_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="attach_warning"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With