Alk
Alk

Reputation: 5557

Attaching layout directly to listview bottom

I have three layouts in my page. Two linearlayouts which represent 2 stripes with information in them and a listview in the third linear layout. I succesfully attached one of the linearlayouts to the top of the page, however I want the second linearlayout (bottom control bar) to be attached to the bottom of the listview, NOT to the bottom of the screen. Can someone please help me fix this... Here is my XML code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey05"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.albert.betterapp.MyBets">

<LinearLayout
    android:id="@+id/top_control_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#C9C193"
    android:orientation="horizontal"
    android:weightSum="1">

<TextView
    android:id="@+id/textView91"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Accumulator(5)"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="16dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView101"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="Token:"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="16dp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/textView121"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:layout_gravity="top"
    android:layout_weight="0.2"
    android:layout_marginBottom="-5dp"
    android:includeFontPadding="false"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/wingreen"
    android:textStyle="bold" />


</LinearLayout>
<LinearLayout
    android:id="@+id/bottom_control_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#C9C193"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="@string/stake"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="500"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:layout_gravity="top"
        android:layout_weight="0.2"
        android:layout_marginBottom="-5dp"
        android:includeFontPadding="false"
        android:text="@string/potentialwinnings"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/wingreen"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="6000"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold" />



</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_above="@id/bottom_control_bar"
    android:layout_alignParentRight="true"
    android:layout_below="@id/top_control_bar"
    android:orientation="vertical">
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@+id/betslistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/betcolor"></ListView>

</LinearLayout>

Upvotes: 0

Views: 459

Answers (2)

Eugene H
Eugene H

Reputation: 3568

Okay so I used two different layouts that are identical. One for the footer (when the listview does not populate the entire screen) and one for the bottom view(when the listview is larger than the screen). Keep in mind that they are two different views but identical. That means you will need to give each item a different id.

First the screen shots:

ListView Doesn't populate entire screen

enter image description here

ListView Does populate entire screen

enter image description here

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/top_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#C9C193"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/textView91"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Accumulator(5)"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="16dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView101"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="Token:"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="16dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView121"
            android:layout_width="wrap_content"
            android:layout_height="18dp"
            android:layout_gravity="top"
            android:layout_marginBottom="-5dp"
            android:layout_weight="0.2"
            android:includeFontPadding="false"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold"/>


    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomView"
        android:layout_below="@+id/top_control_bar"/>

    <!-- The View below should be your menu-->

    <LinearLayout
        android:id="@+id/bottomView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#C9C193"
        android:orientation="horizontal"
        android:weightSum="1">

        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Stake"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="16dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="500"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="16dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView12"
            android:layout_width="wrap_content"
            android:layout_height="18dp"
            android:layout_gravity="top"
            android:layout_marginBottom="-5dp"
            android:layout_weight="0.2"
            android:includeFontPadding="false"
            android:text="potentialwinnings"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="6000"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="16dp"
            android:textStyle="bold"/>
    </LinearLayout>
</RelativeLayout>

footer.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#C9C193"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="Stake"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="500"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:layout_gravity="top"
        android:layout_marginBottom="-5dp"
        android:layout_weight="0.2"
        android:includeFontPadding="false"
        android:text="potentialwinnings"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="6000"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="16dp"
        android:textStyle="bold"/>
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final ArrayList<String> items = new ArrayList<String>();
        for (int i = 0, z = Cheeses.CHEESES.length; i < z; i++) {
            items.add(Cheeses.CHEESES[i]);
        }
        final MyListAdapter myListAdapter = new MyListAdapter(items);

        // BottomView when the list is greater than the screen
        final LinearLayout bottomView = (LinearLayout) findViewById(R.id.bottomView); // Bottom View
        bottomView.setVisibility(View.GONE);
        // FooterView when the list is smaller than the screen
        final View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);

        TextView textView9Footer = (TextView) footerView.findViewById(R.id.textView9); // How you reference the textview in the footer
        textView9Footer.setText("Testing One"); // Updating text in the footer

        TextView textView9 = (TextView) findViewById(R.id.textView9); // Textview in the bottom view
        textView9.setText("Testing Two");

        // Set the adapter to start to check to see if the listView is greater than the screen
        final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(myListAdapter);

        // Runnable to check to see if the list is larger than the screen
        Runnable fitsOnScreen = new Runnable() {
            @Override
            public void run() {
                int last = listView.getLastVisiblePosition();
                if (last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) { // List Not Full Screen
                    bottomView.setVisibility(View.GONE);
                    // Add the footer than reset the adapter to attack the footer
                    listView.addFooterView(footerView);
                } else {
                    // Does nothing other than make the bottom view visible.
                    bottomView.setVisibility(View.VISIBLE);
                }
            }
        };
        listView.post(fitsOnScreen);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    class MyListAdapter extends ArrayAdapter<String> {

        MyListAdapter(ArrayList<String> items) {
            super(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup container) {
            View view = super.getView(position, convertView, container);
            return view;
        }
    }

    public static class Cheeses {
        public static final String[] CHEESES = {
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
            "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
            "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
            "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
            "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss"
        };
        public static final String[] CHEESEStwo = {
            "Abbaye de Belloc", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
            "Abondance", "Abbaye de Belloc"};
    }
}

UPDATE

Possible solution is to put it within PopulateList()

private void PopulateList() {
    ArrayAdapter<BetParameters> adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.betslistview);
    list.setAdapter(adapter);
    final LinearLayout bottomView = (LinearLayout) findViewById(R.id.bottomView); // Bottom View
    bottomView.setVisibility(View.GONE);
    // FooterView when the list is smaller than the screen
    final View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);

    // Runnable to check to see if the list is larger than the screen
    Runnable fitsOnScreen = new Runnable() {
        @Override
        public void run() {
            int last = list.getLastVisiblePosition();
            if (last == list.getCount() - 1 && list.getChildAt(last).getBottom() <= list.getHeight()) { // List Not Full Screen
                bottomView.setVisibility(View.GONE);
                // Add the footer than reset the adapter to attack the footer
                list.addFooterView(footerView);
            } else {
                // Does nothing other than make the bottom view visible.
                bottomView.setVisibility(View.VISIBLE);
            }
        }
    };
    list .post(fitsOnScreen); 
}

Upvotes: 2

Darshan Dorai
Darshan Dorai

Reputation: 659

In your second Linear Layout(bottom control bar), remove

android:layout_alignParentBottom="true"

and replace it with

android:layout_below="@+id/betslistview"

In your third Linear Layout containing the list view, remove

android:layout_above="@id/bottom_control_bar"

Upvotes: 1

Related Questions