7

I'm using a paginator with a few Fragments.

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

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;
    }
6
  • Is there any reason for setting the visibility of the TextView that you create in the noItems() method to View.GONE? Commented Dec 29, 2012 at 15:17
  • I copy down this code from here stackoverflow.com/questions/4088711/…. By the way, I tried remove this line and also set to VISIBLE, but still the same. Commented Dec 29, 2012 at 16:57
  • 1
    Can you try setting the empty TextView only once(for example in the onActivityCreated) in your ListFragment and then simply update its text(look for the TextView with findViewById()(of course adding an id(like android.R.id.empty)))? Commented Dec 29, 2012 at 17:02
  • If I correct understand you I can't update text using findviewById() method because Eclipse shows this error: The method findViewById(int) is undefined for the type FragmentValue. P.S. FragmentValue is the name of the class. Commented Dec 29, 2012 at 17:12
  • Yes, use the Activity: getActivity().findViewById Commented Dec 29, 2012 at 17:19

2 Answers 2

25

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)));
}
Sign up to request clarification or add additional context in comments.

3 Comments

This worked more reliably for me when inserting the emptyView as the first child: ((ViewGroup) getListView().getParent()).addView(emptyView, 0); so that it doesn't overlap the ListView.
It worked like a charme. I agree with @TalkLittle, it's far more reliable than using custom view into layout. Thank you!
After a tiring search... finally above solution worked for me! Thanks @fahmy!
2
  android:id="@android:id/empty" 

and not

  android:id="@id/android:empty"

1 Comment

Still the same. By the way, Android documentation writes @id/android:empty, not @android:id/empty. Reference - developer.android.com/reference/android/app/ListFragment.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.