50

Sorry for the huge code dump, but I'm truly lost.

MyActivity.java onCreate:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();

PlacesFragment.java onCreateView:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;

Notes: mRootView is a ViewGroup global, no problem about it, I believe. PlacesFragment is a ListFragment.

Layouts:

activity_singlepane_empty.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f">
    <include layout="@layout/actionbar"/>

    <!-- FRAGMENTS COME HERE! See match_parent above -->

</LinearLayout>

list_content.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listContainer"
        android:background="#990"
        >
    
        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />
        
        <TextView android:id="@id/android:empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:text="@string/no_data_suggest_connection"/>

</FrameLayout>

Problem: as you can see, the expected behavior would be to have the empty TextView above to appear centered on the screen. On the design preview in Eclipse, it is OK. Only when added to root_view as a fragment the FrameLayout won't fill the whole screen.

root_container is blue, and FrameLayout is yellowish, see below for debug purposes. Shouldn't the yellow pane fill the whole screen?!?!?!?

enter image description here

Tugay
  • 2,057
  • 5
  • 17
  • 32
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • If you use ScrollView checkout this: http://stackoverflow.com/questions/10211338/view-inside-scrollview-doesnt-take-all-place – Gelldur May 10 '15 at 15:45

6 Answers6

76

I had the same problem and think it happens when you inflate the layout in the Fragment's onCreateView with null, like you did here:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);

Instead you have to do this:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);

Where container is the Viewgroup. At least, that solved the problem for me.

Norman
  • 2,351
  • 1
  • 20
  • 16
  • 2
    http://developer.android.com/reference/android/view/LayoutInflater.html Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) Answer accepted. Thank you. – davidcesarino Mar 13 '12 at 01:56
  • 2
    see also http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater – Stevie Aug 21 '13 at 13:00
  • What ViewGroup is container? root_container from activity_singlepane_empty.xml? – The incredible Jan Feb 20 '23 at 07:42
6

For some reason the FrameLayout does not get its layout from the XML.

I need to set it also in code (Fragment's onCreateView):

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
FrameLayout fl = (FrameLayout) mRootView.findViewById(R.id.listContainer);
fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return mRootView;
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • This is an old answer. Since then, I switched the acceptance mark to Norman's answer. Of course this answer provides more flexibility, but I'm glad to accept his since this question clearly warranted a solution for easy-of-use. – davidcesarino Mar 13 '12 at 01:58
5
<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    >

    <include layout="@layout/screen_main_fragment" />

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

I had to change layout_height="wrap_content" to "match_parent"to make it work.

user1070356
  • 217
  • 3
  • 2
0

The way I did that was to create a transparent image in xml code, make the fragment a relative layout and makelayout_alignParentBottom="true" in the ImageView, this way the image sticks to the bottom and makes the fragment fill the parent

Code Here:

temp_transparent.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:thickness="0dp"
   android:shape="rectangle">
<gradient
    android:startColor="#00000000"
    android:endColor="#00000000"
    android:type="linear"
    android:angle="270"/>

fragment_my_invites.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:id="@+id/my_invites_fragment"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#ffffff">


<ListView
    android:id="@+id/list_invites"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:layout_alignParentTop="true" >
</ListView>

<ImageView
    android:id="@+id/transparent_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/list_invites"
    android:src="@drawable/temp_transparent" />

Diogo Peres
  • 1,302
  • 1
  • 11
  • 20
0

I had the same problem & i tried many things but none of them worked.

What problem faced?

I had <FrameLayout> inside <ScrollView> which height is set to 0dp which causes the problem.

<ScrollView
   android:id="@+id/scrollView2"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:background="@color/white"
   app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/myToolbar">

      <FrameLayout
        android:id="@+id/mainFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myToolbar" />
</ScrollView>

To make it working change all the outer containing views to match_parent

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RHS.Dev
  • 412
  • 6
  • 18
0

Adding this line to the Parent Layout will solve that problem,

android:fillViewport="true"