Empty State Screen

The Empty State Screen pattern will show a predefined default screen when we can't load data from the Internet (online) or from the SQLite database (offline). This can happen, for example, when a user launches the app for the first time while having no Internet connectivity.

To do this, we will modify the original activity_main.xml layout file to contain a message that is shown when there is no data:

<TextView
android:id="@+id/no_data_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="We are sorry, but we couldn't load any data."
android:textAlignment="center"
android:textSize="26sp"
android:visibility="gone" />
Here, we are not using extracted strings in the XML for the sake of simplicity. Ideally, they should be moved to the strings.xml file.

It's a TextView that's hidden by default so that it doesn't get in the way while we are fetching data. However, if the fetch failed, and there is no data to display, we are ready to show it.

First of all, let's inject a component into our activity by adding the following:

@BindView(R.id.no_data_available)
TextView noDataAvailableView;

Next, we will modify the current .subscribe() block to handle the hiding and showing of the empty screen:

.subscribe(stockUpdate -> {
Log.d("APP", "New update " + stockUpdate.getStockSymbol());
noDataAvailableView.setVisibility(View.GONE);
stockDataAdapter.add(stockUpdate);
}, error -> {
if (stockDataAdapter.getItemCount() == 0) {
noDataAvailableView.setVisibility(View.VISIBLE);
}
});

If there is an exception that managed to reach the .subscribe() block, it will check whether there is any data in the adapter and if there are none, the empty view will be shown. Otherwise, we ensure that the empty state view is hidden by calling the following and add the data to the RecyclerView:

noDataAvailableView.setVisibility(View.GONE);

It's a very good and important practice to provide Empty State Screen so that the user is told explicitly that the data isn't loaded and that it isn't somewhere in the transition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.220.22