Skip to content

Commit

Permalink
1.18 Add empty view to the ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent feca5f2 commit 636e6bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public void onClick(View view) {
startActivity(intent);
}
});

// Find the ListView which will be populated with the pet data
ListView petListView = (ListView) findViewById(R.id.list);

// Find and set empty view on the ListView, so that it only shows when the list has 0 items.
View emptyView = findViewById(R.id.empty_view);
petListView.setEmptyView(emptyView);
}

@Override

This comment has been minimized.

Copy link
@aitrenI

aitrenI Apr 21, 2020

Why didn't we set the visibility of emptyView into GONE and still disappears when there is some data in the cursor?

This comment has been minimized.

Copy link
@raulserranomena

raulserranomena Jun 17, 2020

because:

The ListView class or more generally the AdapterView class (Gallery, GridView, etc.) handle the empty case in a very simple way. AdapterView exposes a method called setEmptyView(View) that binds a View to itself. Once an empty view has been set, the AdapterView hides/displays it according to the current state (empty or not) of the underlying Adapter.

The AdapterView and the empty view are mutually exclusive in term of visibility: if the empty view is visible (View.VISIBLE) the AdapterView has the View.GONEvisibility and vice-versa.AdapterView` determines its emptiness by looking at the following conditions:

The underlying Adapter is null
A call to isEmpty() on the underlying Adapter returns true

Source: https://cyrilmottier.com/2011/06/20/listview-tips-tricks-1-handle-emptiness/

Expand Down
38 changes: 38 additions & 0 deletions app/src/main/res/layout/activity_catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<!-- Empty view for the list -->
<RelativeLayout
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<ImageView
android:id="@+id/empty_shelter_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_empty_shelter"/>

<TextView
android:id="@+id/empty_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/empty_shelter_image"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="@string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium"/>

<TextView
android:id="@+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/empty_title_text"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="@string/empty_view_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0"/>
</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<!-- Label for overflow menu option that deletes all pet data in the app [CHAR LIMIT=20] -->
<string name="action_delete_all_entries">Delete All Pets</string>

<!-- Title text for the empty view, which describes the empty dog house image [CHAR LIMIT=50] -->
<string name="empty_view_title_text">It\'s a bit lonely here...</string>

<!-- Subtitle text for the empty view that prompts the user to add a pet [CHAR LIMIT=50] -->
<string name="empty_view_subtitle_text">Get started by adding a pet</string>

<!-- Title for the activity to add a new pet [CHAR LIMIT=20] -->
<string name="editor_activity_title_new_pet">Add a Pet</string>

Expand Down

3 comments on commit 636e6bd

@BasemSaabneh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work

@KushanTharaka97
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In sixth video you will face huge error because of oudated method
so, the out dated method is
getLoaderManager().initLoader(0, null, this);
you can use
LoaderManager.getInstance(this).initLoader(0, null, this); instead in 2020
/**

  • @deprecated Use
  • {@link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
    */

[/**

  • @deprecated Use
  • {@link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
    */](url)

@abdallah-prog
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In sixth video you will face huge error because of oudated method
so, the out dated method is
getLoaderManager().initLoader(0, null, this);
you can use
LoaderManager.getInstance(this).initLoader(0, null, this); instead in 2020
/**

* @deprecated Use

* {@link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
  */

[/**

* @deprecated Use

* {@link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
  */](url)

i signed to this site to say thank you very much to you

Please sign in to comment.