Question

So i have recently been working on switching my application over to using Fragments to make it a bit more flexible. One thing I have noticed is that when I inflate my fragment view, it is not taking up the entire screen, there is still a gap at the top (below actionbar) that is actually part of my activity_main.xml

here are my view and where i inflate.

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@color/mainBlue"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/appTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:textColor="@color/white"
        android:gravity="center_horizontal"
        android:textSize="75sp"
        android:layout_weight="2"/>

    <TextView
        android:id="@+id/newRecord"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/newRecord"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_horizontal"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/pastRecords"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/pastRecords"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_horizontal"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/statistics"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/statistics"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_horizontal"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/settings"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/settings"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_horizontal"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1" />

</LinearLayout>

<!-- The navigation drawer -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"       
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

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

new_record.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/mainBlue">
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1" >

        <EditText
            android:id="@+id/pricePerGallon"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/pricePerGallon"
            android:inputType="numberDecimal"
            android:textSize="30sp"
            android:textColor="@color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
    android:layout_weight="1">

            <requestFocus />
        </EditText>


        <EditText
            android:id="@+id/gallons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/gallons"
            android:inputType="numberDecimal"
            android:textSize="30sp"
            android:textColor="@color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
    android:layout_weight="1"/>

    <EditText
        android:id="@+id/odometer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/odometer"
        android:inputType="numberDecimal"
        android:textSize="30sp"
        android:textColor="@color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
    android:layout_weight="1"/>

    <EditText
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="date"
        android:textSize="30sp"
        android:textColor="@color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
    android:layout_weight="1"/>

    <EditText
        android:id="@+id/comments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="30sp"
        android:textColor="@color/white"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
        android:hint="@string/comments" 
    android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/completeFill"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/notCompleteFill"
        android:textColor="@color/white"
    android:layout_weight="1" />
</LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/mainBlue"
    android:gravity="center_horizontal" >
    <TextView
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1"
        android:gravity="center_horizontal"/>

    <TextView
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        android:textSize="50sp"
        android:textColor="@color/white"
        android:layout_weight="1"
        android:gravity="center_horizontal" />
    </LinearLayout>
</LinearLayout>

MainActivity.java (inflating fragment here)

if (fragment != null) {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
    .replace(R.id.main_container, fragment).commit();


    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
} else {
    // error in creating fragment
    Log.e("MainActivity", "Error in creating fragment");
}

Funny thing is, if I use an intent and call my NewRecord.java class the view works just fine. If I inflate my NewRecordFragment.java which is replacing NewRecord.java, is when I have the issue.

Any ideas?

Was it helpful?

Solution

The reason for the space is because your container layout (the layout into which the Fragment is inflated) has paddings applied:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@color/mainBlue"
    android:orientation="vertical" >

Remove the paddings at top, bottom, left and right and your Fragment will take up all the space.

OTHER TIPS

I would remove these lines:

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top