0

in my android app I have the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_tab"
android:clipToPadding="false">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listaNews1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:divider="@color/transparent"></ListView>

    <LinearLayout
        android:id="@+id/ll_news_empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="gone"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/news_icon"
            android:tint="@color/gray_dark"
            android:scaleType="centerCrop" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nessuna news da mostrare"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/gray_dark"/>

    </LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

And programmatically I have this code to add the empty view:

...
listaNovità = (ListView)view.findViewById(R.id.listaNews1);

listaNovità.setEmptyView(view.findViewById(R.id.ll_news_empty_view));
...

But executing the code on emulator I see an empty layout (the listview is empty and the empty view is not showing). What is the reason and how can I fix the problem?

Thanks everyone wants to help me.

7
  • Is this in a Fragment? If so, are you certain you're returning the View you're doing that setup on?
    – Mike M.
    Commented Mar 25, 2018 at 12:53
  • I'm in a View Pager. Yes I'm sure because I have a lot of successful logic on that..adding "setEmptyView" appears the strange behavior on the layout
    – Claudio P
    Commented Mar 25, 2018 at 13:01
  • It seems to be due to the SwipeRefreshLayout; rather, due to having both things as direct children of it. I'll have to look into that, but if you wrap the ListView and the LinearLayout in a FrameLayout, it should work.
    – Mike M.
    Commented Mar 25, 2018 at 13:11
  • Oh, yeah, the docs even say "can only support one direct child", and if you look at the source, it only measures and lays out its first child; i.e., getChildAt(0). Any other children are ignored, so they're basically invisible.
    – Mike M.
    Commented Mar 25, 2018 at 13:16
  • 1
    Thank you, using another Layout (eg like FrameLayout as you suggested) it works fine.
    – Claudio P
    Commented Mar 25, 2018 at 13:32

0

Browse other questions tagged or ask your own question.