Search code examples
androidandroid-layoutandroid-fragmentsandroid-listfragment

Unable to get resource ID for Android view


I'm creating a custom ListFragment for my Android application. I want to add a child to @id/listContainer. But I'm not able to get the reference to the view using findViewById(android.R.id.listContainer).

This is the default layout for ListFragment.

list_content.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <ProgressBar style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/loading"
                android:paddingTop="4dip"
                android:singleLine="true" />

    </LinearLayout>

    <FrameLayout android:id="@+id/listContainer"
            android:layout_width="match_parent" 
            android:layout_height="match_parent">

        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />
        <TextView android:id="@+android:id/internalEmpty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    </FrameLayout>

</FrameLayout>

This is my custom ListFragment code.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(android.R.layout.list_content, container, false);

    ProgressBar progressBar = new ProgressBar(getActivity());
    progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
    progressBar.setIndeterminate(true);

    ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
    listContainer.addView(progressBar);

    return view;
}

Android Studio says error: cannot find symbol variable listContainer.

What I am doing wrong?


Solution

  • Try getListView().getParent(). since getListView() gives you the list view widget,it's parent should be the frame layout that you need.