Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView / ExpandableListView setEmptyView() has no effect

I'm trying to set empty view for three list views, namely 2 expandable and one list view, which are in different tabs:

LayoutInflater inflater = getLayoutInflater();
        View emptyView = inflater.inflate(R.layout.empty_view, null, false);
...
ExpListView1.setEmptyView(emptyView);
ListView.setEmptyView(emptyView);
ExpListView2.setEmptyView(emptyView);

It does no effect - empty view doesn't appear. Same with that code piece:

View emptyView = inflater.inflate(R.layout.empty_view, null, false);
View emptyRelativeLayout = (RelativeLayout) emptyView.findViewById(R.id.empty_view_layout);
ExpListView1.setEmptyView(emptyRelativeLayout);

empty_view_layout.xml:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/empty_view_layout">
    <TextView
    style="@android:style/TextAppearance.Large"
    android:id="@+id/empty_view_textview"
    android:layout_width = "fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/no_items_in_list" 
    android:layout_centerInParent="true"/>
</RelativeLayout>
like image 242
87element Avatar asked Sep 13 '11 10:09

87element


2 Answers

The problem was solved by adding empty view as another content view to my Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    inflater = getLayoutInflater();
    emptyView = inflater.inflate(R.layout.empty_view, null);
    addContentView(emptyView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
like image 172
87element Avatar answered Nov 03 '22 23:11

87element


I have done this way:

activity_main.xml

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expListView">

        </ExpandableListView>


        <TextView
            android:id="@+id/txtEmptyView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/no_game_available"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@android:color/white" />

</RelativeLayout>

MainActivity.java

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

TextView txtEmptyView = (TextView) view.findViewById(R.id.txtEmptyView);

expandableListView.setEmptyView(txtEmptyView);

Hope this will help you.

like image 32
Hiren Patel Avatar answered Oct 03 '22 08:10

Hiren Patel