Correct use of setEmtpyView in AdapterView

setonitemclicklistener in adapterview cannot be applied
how is an adapter bound to an adapter view
how to access adapter view in activity in android
android listview onitemclicklistener get item
onitemclicklistener is abstract cannot be instantiated

I'm really having trouble using the setEmptyView method. I tried it to implement it in GridView and ListView, but both of them didnt work. Here a sample codeblock:

 networkGames = (GridView) baseLayer.findViewById(;
 networkGames.setOnItemClickListener(new NetworkGameListener());
 networkGames.setEmptyView(View.inflate(baseLayer, R.drawable.no_network_games, null));
 networkGames.setAdapter(new NetworkAdapter());

The network adapter contains no items:

private class NetworkAdapter extends BaseAdapter {

        /* (non-Javadoc)
         * @see android.widget.Adapter#getCount()
        public int getCount() {
            return 0;

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
        public Object getItem(int position) {
            return null;

        /* (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
        public long getItemId(int position) {
            return 0;

        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;


I also tried to call networkGames.setAdapter(null), but this doesnt work either. My emtpyView looks like this:

    android:text="There are currently no network games available. Start a new one."

I really don't know what I'm doing wrong. I also read various tutorials, but none of them metnioned any problems.

You need to add this same view in the layout in which you have added the AdapterView.

AdapterView only changes its visibility based on the contents in the adapter.

EDITED : Following layout and code works fine :

        android:id="@+id/list_view" />
        android:text="List view is empty"
        android:visibility="gone" />

Code :

ListView listView = (ListView) findViewById( );
listView.setEmptyView( findViewById( ) );
listView.setAdapter( new ArrayAdapter( this, R.layout.selected_spinner_view, new ArrayList() ) );

AdapterView, void, onLayout(boolean changed, int left, int top, int right, int bottom). Called from layout when this view should assign a size and position to each of its children. android - Correct use of setEmtpyView in AdapterView . I'm really having trouble using the setEmptyView method. I tried it to implement it in GridView and ListView, but both of them didnt work. Here a sample codeblock: networkGames=(GridView) baseLayer…

Karan's answer is spot-on. The AdapterView indeed displays the empty view by merely changing its own visibility vs the empty view's visibility. However, the empty view layout does not necessarily need to be in the same XML file as that of the AdapterView. I found it useful to separate the two because I use a single layout XML file (containing a GridView) for multiple activities, but I wanted to specify different emptyView layouts for each of them.

The key is to add the empty view to the activity with addContentView. Remember to do this after you have called setContentView specifying your main AdapterView.

My code (in AbstractBaseActivity.onCreate) :

View mainView = inflater.inflate(R.layout.imagegrid, null);
GridView gridView = (GridView)mainView.findViewById(;
View emptyView = inflater.inflate(getIdOfEmptyView(), null, false);
// Any additional processing on mainView
addContentView(emptyView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

In each child activity I override getIdOfEmptyView as follows:

protected int getIdOfEmptyView()
    return R.layout.emptyView_Favorites;

How to search for items in a 2D ArrayList used in Android?, You can use setEmptyView function available in ListView. Please refer to this for more info: Correct use of setEmtpyView in AdapterView  android – Correct use of setEmtpyView in AdapterView Tags ajax android angular api button c++ class database date dynamic exception file function html http image input java javascript jquery json laravel list mysql object oop ph php phplaravel phpmysql phpphp post python sed select spring sql string text time url view windows wordpress xml

You can define external XML layout files, inflate and set it as an empty view. Only thing to remember is to set the parent of the inflated view.

View emptyView = inflater.inflate(R.layout.id_of_your_layoout_file, parent, false);



Open Image from listView when item is clicked, But I'm stuck in how I can pass this string value to the new activity and use it to For examples look at this question Correct use of setEmtpyView in AdapterView  El uso correcto de setEmtpyView en AdapterView Realmente estoy teniendo problemas con el uso de la setEmptyView método. He probado a ponerlo en práctica en GridView y ListView, pero ambos de ellos funcionó.

PacificSky's answer got me almost to what I needed, but adding the empty view to the activity with addContentView caused the empty view to remain in the activity's container even after removing the fragment containing the ListView from the activity. Additionally, the slide in menu for the activity displayed under or mixed in with the empty view.

The solution for me was to add the empty view to the container passed in as a parameter to the fragment's onCreateView:

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    mFragmentContainer = container;
    mListView = (ListView) mLayout.findViewById(;
    mEmptyListView = inflater.inflate(R.layout.empty_list, null, false);
    mListAdapter = new MyListAdapter(getActivity(), mList);

And remove the empty view from the container in the fragment's onDestroyView:

public void onDestroyView(){

2 Correct use of setEmtpyView in AdapterView Oct 16 '12. 1 Android WebView doesn't finish Jun 19 '13. 0 Android - ImageButton onClick event fails to fire Jun 24 '13.

For my case, gridView.setEmptyView(emptyView) was called but it's not show the emptyview. Unless I called gridView.setVisibility(View.VISIBLE), so it appear normally;

93 Correct use of setEmtpyView in AdapterView May 11 '10 83 Get thumbnail Uri/path of the image stored in sd card + android Apr 12 '11 48 onAnimationEnd is not getting called, onAnimationStart works fine Mar 29 '11

4 . this is not really ambiguous, you just have to understand the scopes / closures. nevertheless, if switchLabel is a generic function, you shouldn't use this, if its only use, now and forever, is as a callback/handler to a (jQuery) event, you can rely on this

AdapterView.AdapterContextMenuInfo: Extra menu information provided to the onCreateContextMenu(ContextMenu, View, ContextMenuInfo) callback when a context menu is brought up for this AdapterView. interface: AdapterView.OnItemClickListener: Interface definition for a callback to be invoked when an item in this AdapterView has been clicked. interface

The documentation for AdapterView should really be updated to reflect this, since it is not obvious. The simplest way to use setEmptyView() is with your "empty" TextView and ListView in the same layout like this:

  • i added the above layout at the end of the layout where i define my gridview. but nevertheless it isn't displayed
  • hmm is it possible that i have to define the gridview and the emptyview exclusively (with no other layout items like buttons) in a separate xml file?
  • I've modified the answer with the example. It works fine on Android 2.1. HTH !
  • This helped me in a slightly unrelated situation. The fact that AdapterView just changes visibility instead of actually managing which View is presents to its parent layout is the key!
  • This doesnt work if you are using
  • what if you don't want the whole activity to be overridden with your empty view (like headers or buttons)?
  • One option might be to wrap both the GridView and whatever empty view you have in a parent layout (like a LinearLayout or RelativeLayout in the activity's view). That will ensure that they occupy at most as much space as the parent. The parent itself could be laid out in the activity view using a RelativeLayout, ensuring that headers, buttons etc are correctly shown independently of the GridView/empty view.
  • Awesome. That addContentView line was exactly what I was missing in my code. Thanks!
  • What about for a Spinner and what if you're calling that function outside AdapterView without a parent object? LayoutParams didn't work for me an my Spinner shrinks/disappears when empty.