Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom empty view in ListView android

I want to show a refresh Button and a TextView if there is no data in the ListView adapter. I also want to be able to add a click listener to the button that will reload the list. Here is how I have my current activity defined:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.foods);

    arrList=new ArrayList<FoodsData>();

    listView=(ListView)findViewById(R.id.list_foods);

    this.listView.setEmptyView(findViewById(R.id.emptyView));

    .....
    .....
}

Here is my xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:id="@+id/emptyView">
    <TextView
        android:layout_width="wrap_content"
        android:textColor="@android:color/white"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Click Refresh to load data"/>
   <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_retry"
        android:textColor="@android:color/white"
        android:background="@drawable/orange_button_selecter"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:text="@string/retry"/>
</LinearLayout>
<ListView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:dividerHeight="2dp"
    android:id="@+id/list_foods"/>

Where would I set the click listener for my refresh Button?

like image 907
Lalit Sharma Avatar asked Dec 26 '22 05:12

Lalit Sharma


1 Answers

Change your xml like below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
    <FrameLayout 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_centerInParent="true"
        android:id="@+id/emptyView">
        <TextView
            android:layout_width="wrap_content"
            android:textColor="@android:color/white"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Click Refresh to load data"/>
       <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_retry"
            android:textColor="@android:color/white"
            android:background="@drawable/orange_button_selecter"
            android:layout_gravity="center"
            android:textSize="25sp"
            android:text="@string/retry"/>
    </FrameLayout>
<ListView 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:dividerHeight="2dp"
    android:id="@+id/list_foods"/>
</RelativeLayout>

put that lines in java

    View empty = findViewById(R.id.emptyView);
    btn_retry=(Button)empty.findViewById(R.id.btn_retry);
    listView.setEmptyView(empty);

Now you can write onClickListener() in same Activity as it belongs with same xml layout.

btn_retry.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //Do the refresh thing or verify with Toast.
            }
        });

Here, If the listview is an empty it will show button refresh. Otherwise it will display the filled list.

like image 67
user1621629 Avatar answered Jan 07 '23 04:01

user1621629