-4

I want to display a textview as a last item of List View. Here is my xml code -

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.activity.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="8dp">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingBottom="60dp" />

        </android.support.v4.widget.SwipeRefreshLayout>

    </FrameLayout>
</LinearLayout>

The list view will display card view one below other. However I want a textview as last item in the list. For example, if i have 1 card view, I need to display textview below that. If I have 2 card views, I need to display textview below the second card view.

How can I achieve this?

Del905241
  • 25
  • 3
  • Show your activity or fragment code please – ImAtWar Jun 01 '17 at 05:58
  • 2
    listview.addfooter https://stackoverflow.com/questions/4265228/how-to-add-a-footer-in-listview – Jaydeep Devda Jun 01 '17 at 05:59
  • 3
    For this, you need to add footer view to your listview. Check this https://stackoverflow.com/questions/3227182/android-adding-footer-to-listview-addfooterview – Sachin Jun 01 '17 at 06:02
  • When I do , I get exception - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams – Del905241 Jun 01 '17 at 14:28

3 Answers3

1

This can be solved by adding a footer to the list view.

It is very simple, just create a layout with the required Textview. Add it as a footer to the list view as below.

listView.addFooterView(footerView);

Please find more reference to adding footer from this Question.

Hariharan Ayyasamy
  • 287
  • 1
  • 2
  • 14
  • When I do , I get exception - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams – Del905241 Jun 01 '17 at 14:28
1

inflate your view like this.

     LayoutInflater layoutInflater = (LayoutInflater) 
     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View root = layoutInflater.inflate(R.layout.yourLayout, null);
     // for footerclick
     //add code her root.setOnClickListner(...)
     listView.addFooterView(root, null, true);

for click try this

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
          if(position < adapter.getCount()){ 
            Log.d("test","working");
          }
     }
 });

on Refresh listview

mListView.removeFooterView(root);
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
0

You can wrap your ListView in LinearLayout and add TextView just below it. Then wrap that with NestedScrollView and set fillViewPort to true to still be able to scroll when TextView goes beyond screen

<LinearLayout 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:orientation="vertical"
    tools:context="com.activity.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="8dp">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <ListView
                        android:id="@android:id/list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="false"
                        android:paddingBottom="60dp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Footer text view" />

                </LinearLayout>

            </android.support.v4.widget.NestedScrollView>

        </android.support.v4.widget.SwipeRefreshLayout>

    </FrameLayout>

</LinearLayout>
matdziu
  • 13
  • 6