0

I am inflating a layout of the fragment. Its not utilising the whole screen and the layout looks like its shrunk to half the screen. But in the preview screen of Android studio looks perfect.I am using navigation component to traverse to the Fragment.

The layout xml is shown below

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".features.user.view.fragment.AddUser">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/delete_tv_border">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/userNameEt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:hint="@string/name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:hint="@string/age"
            app:layout_constraintBottom_toTopOf="@+id/genderEt"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/userNameEt">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/genderEt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:layout_marginBottom="200dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/sex" />
        </com.google.android.material.textfield.TextInputLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>


</layout>

The android studio preview is perfect as shown below

enter image description here

The screenshot does not match the studio preview

enter image description here

I have also observed that, when using an activity it works perfect. But with fragment it is not right. Below is the fragment code where I inflate.

import androidx.fragment.app.Fragment;
/**
 * A simple {@link Fragment} subclass.
 */
public class AddUser extends Fragment {


    public AddUser() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add_user, container, false);
    }

}

Main activity layout is below

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".features.main.view.activity.MainActivity">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="10dp"
                android:background="@color/colorPrimary"
                android:elevation="20dp"
                app:layout_constraintTop_toTopOf="parent"/>
            <fragment
                android:id="@+id/main_nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:defaultNavHost="true"
                app:layout_constraintTop_toBottomOf="@+id/toolbar"
                app:navGraph="@navigation/main_navigation_graph" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:menu="@menu/nav_drawer_menu" />
    </androidx.drawerlayout.widget.DrawerLayout>


</layout>

Please help me resolve this issue.

Vivek
  • 85
  • 8

1 Answers1

0

The height of the fragment in the main activity was wrap content. I changed it to match constraints, which solved my issue.

Thanks ianhanniballake for pointing me to look at the main activity layout

Vivek
  • 85
  • 8