Search code examples
androidlistviewtextviewandroid-linearlayoutandroid-relativelayout

Set textView to Bottom Right


I'm trying to fix two text views to the bottom right and keep it above the listview. If the listview is empty i want it fixed to the bottom right but with this code if i add some items to the listview the textview disappears. Layout Image

<?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">
    <LinearLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout1">
        <Button
            android:text="Fechar Comanda"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnFecharComanda" />
        <Button
            android:text="Extrato"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnExtrato" />
        <Button
            android:text="Enviar Cozinha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnEnviarCozinha" />
        <Button
            android:text="Anterior"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnAnterior" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:weightSum="100">
        <ListView
            android:id="@+id/sportsList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/primeira"
            android:layout_weight="33" />
        <ListView
            android:id="@+id/sportsList_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/segunda"
            android:layout_weight="34" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="33"
            android:id="@+id/linearLay123123"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="100">
            <ListView
                android:id="@+id/sportsList_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/terceira" />
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/odkjfs"
                android:layout_width="match_parent"
                android:layout_height="100px"
                android:gravity="bottom">
                <TextView
                    android:text="Total"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2"
                    android:gravity="end"
                    android:layout_marginRight="10dp" />
                <TextView
                    android:text="R$15"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:id="@+id/textView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="end"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Solution

  • So you've tagged your question with ReleativeLayout, which is what the solution to your problem is. You need to change your linearLay123123 to a RelativeLayout and tell the layout system, that you always want odkjfs at the bottom right and your ListView sportsList_2 above it:

    <?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">
        <LinearLayout
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayout1">
            <Button
                android:text="Fechar Comanda"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnFecharComanda" />
            <Button
                android:text="Extrato"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnExtrato" />
            <Button
                android:text="Enviar Cozinha"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnEnviarCozinha" />
            <Button
                android:text="Anterior"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnAnterior" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1"
            android:weightSum="100">
            <ListView
                android:id="@+id/sportsList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/primeira"
                android:layout_weight="33" />
            <ListView
                android:id="@+id/sportsList_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/segunda"
                android:layout_weight="34" />
            <RelativeLayout
                android:layout_weight="33"
                android:id="@+id/linearLay123123"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ListView
                    android:id="@+id/sportsList_2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_above="@+id/odkjfs"
                    android:entries="@array/terceira" />
                <LinearLayout
                    android:orientation="vertical"
                    android:id="@+id/odkjfs"
                    android:layout_width="match_parent"
                    android:layout_height="100px"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true">
                    <TextView
                        android:text="Total"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView2"
                        android:gravity="end"
                        android:layout_marginRight="10dp" />
                    <TextView
                        android:text="R$15"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:id="@+id/textView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="end"
                        android:layout_marginRight="10dp"
                        android:layout_marginBottom="10dp" />
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
    

    You Id's are very hard to remember so I advise you to change them.

    But lets break down the layout above. You have an outer layout with two rows. The bottom one has three columns. What you wanted to change is rightmost column so that it persists something on the bottom of it.

    So the easiest thing to do here is to change from a LinearLayout to a RelativeLayout, since it is much easier to align views relative to each other in it, as the name indicates.

    So after changing it to RelativeLayout we need to tell the ListView to align to the top. And that it needs to adjust its height to be above the content below.

    android:layout_alignParentTop="true"
    android:layout_above="@+id/odkjfs"
    

    Then on the LinearLayout which you wanted to be on bottom right. We need to tell it to adjust right, and adjust to bottom:

    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    

    Simple as that.