How to use setEmptyView() with custom list layout in ListFragment

I’m using a paginator with a few Fragments.

I want to show a message when there are no entries in the list (Fragment).

  • New Android Gradle Build System build configs package name conflicts with Provider Authority
  • onOptionsItemSelected not called when using actionLayout (SherlockActionBar)
  • Streaming audio between external bluetooth device and Android phone
  • How to pass Bundle from Fragment to Fragment
  • Is it possible to get access to the speaker signal on Android?
  • Triggering a PopupMenu with buttons inside a ListView item
  • I tried to use Textview with android:id="@id/android:empty but it doesn’t work.

    Custom list layout code:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium" >
    
        <TextView
            android:id="@+id/label_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/padding_medium"
            android:text="@string/label_value"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
    
        <TextView
            android:id="@+id/label_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/padding_medium"
            android:text="@string/title_time"
            android:textColor="@android:color/white"
            android:textSize="16sp" />
    
    </RelativeLayout>
    

    List adapter class:

    public class JournalAdapter extends ArrayAdapter<String> {
    
        private final Context context;
        private final List<String> values;
        private final List<String> dates;
    
        public JournalAdapter(Context context, List<String> values,
                List<String> dates) {
            super(context, R.layout.layout_journal_list, values);
    
            this.context = context;
            this.values = values;
            this.dates = dates;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View rowView = inflater.inflate(R.layout.layout_journal_list, parent, false);
    
            TextView value = (TextView) rowView.findViewById(R.id.label_glucose);
            TextView date = (TextView) rowView.findViewById(R.id.label_date);
    
            value.setText(values.get(position));
            date.setText(dates.get(position));
    
            return rowView;
        }
    
    }
    

    List parsing method:

    private void initListView() {
            values = dataSource.getAllValues();
    
            List<String> values = new ArrayList<String>();
            List<String> dates = new ArrayList<String>();
    
            for (Value value : values) {
                values.add(Double.toString(value.getValue()));
                dates.add(secondsToDate(value.getDate()));
            }
    
            setListAdapter(new JournalAdapter(context, values, dates));
    
            listView = getListView();
            listView.setEmptyView(noItems("NO ITEMS"));
            listView.setOnItemClickListener(this);
        }
    

    noItems method:

    private TextView noItems(String text) {
            TextView emptyView = new TextView(context);
            emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            emptyView.setText(text);
            emptyView.setTextColor(Color.WHITE);
            emptyView.setTextSize(20);
            emptyView.setVisibility(View.GONE);
            emptyView.setGravity(Gravity.CENTER_VERTICAL
                    | Gravity.CENTER_HORIZONTAL);
    
            return emptyView;
        }
    

    Related posts:

    Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK
    Testing database on Android: ProviderTestCase2 or RenamingDelegatingContext?
    Loopj Android Async Http - onFailure not fired
    How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL ...
    Specifying &ldquo;strikethrough&rdquo; on a section of TextView text
    How to enable/disable WiFi from an application?
  • Android Draw 9-patch tool: What is the meaning of black lines outside the figure
  • How can I show the soft keyboard in a webview with a USB HID device attached when a button is clicked and an input is focused?
  • How to set dialog's position?
  • Decoupling In-App-Billing from Activity
  • Android ADB access to application databases without root
  • Creating a setError() for the Spinner
  • 2 Solutions collect form web for “How to use setEmptyView() with custom list layout in ListFragment”

    This worked for me (got the solution from this question asked by William Remacle). What you might have missed is adding the created TextView to the ListView layout parent (second line of code from the bottom of the noItems method below):

    In my ListFragment I used the following method to get a view for the empty view:

    private TextView noItems(String text) {
        TextView emptyView = new TextView(getActivity());
        //Make sure you import android.widget.LinearLayout.LayoutParams;
        emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        //Instead of passing resource id here I passed resolved color 
        //That is, getResources().getColor((R.color.gray_dark))
        emptyView.setTextColor(getResources().getColor(R.color.gray_dark));
        emptyView.setText(text);
        emptyView.setTextSize(12);
        emptyView.setVisibility(View.GONE);
        emptyView.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);
    
        //Add the view to the list view. This might be what you are missing
        ((ViewGroup) getListView().getParent()).addView(emptyView);
    
        return emptyView;
    }
    

    Then in the onStart() method of the the ListFragment set the the empty view:

    @Override
    public void onStart() {
        super.onStart();
        getListView().setEmptyView(
                noItems(getResources().getString(R.string.widget_empty)));
    }
    
      android:id="@android:id/empty" 
    

    and not

      android:id="@id/android:empty"
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.