Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active February 10, 2018 22:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightfoot/5519281 to your computer and use it in GitHub Desktop.
Save slightfoot/5519281 to your computer and use it in GitHub Desktop.
Example of the use of a ListView with associated empty View.
<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=".MainActivity"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/refresh"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/empty"
/>
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="empty">Empty</string>
<string name="refresh">Refresh</string>
</resources>
import java.util.ArrayList;
import java.util.Random;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
public class TestActivity extends ListActivity
{
public static Intent createIntent(Context context)
{
return new Intent(context, TestActivity.class);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
refreshAlbumList();
}
});
refreshAlbumList();
}
public void refreshAlbumList()
{
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getAlbumList()));
}
private ArrayList<String> getAlbumList()
{
ArrayList<String> list = new ArrayList<String>();
if(new Random().nextBoolean()){
list.ensureCapacity(100);
for(int i = 0; i < 100; i++){
list.add("Album Name " + (i + 1));
}
}
return list;
}
}
@HologramOfMe
Copy link

I would have expected to see a call to setEmptyView() to really showcase how this works. But maybe ListActivity is able to automatically detect that there is a view with the special android empty id and does it for you. Is that the case?

@seupedro
Copy link

@HologramOfMe I really agree with you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment