Search code examples
javaandroidlayout-inflater

Fragment not completely inflating


I've created an Activity that has three tabs. On clicking Tab 1 it opens up a fragment (TaskFragment) containing two buttons, each with an associated text.

When I click on the "Create Appraisal Report" button it properly inflates another Fragment's (AppraisalReportFragment) two text lines (that now supercede the previous two), and a page title (Appraisal Reports), but the two initial buttons remain in place.

I'm trying to get everything on the initial Fragment to be changed over to an entirely new Fragment, with it's own buttons and text), but right now only some of these changes take place.

TaskFragment

package usjersey.com.jerseyscore.fragments;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import usjersey.com.jerseyscore.R;

/**
 * Created by dhiggins on 11/20/2017.
 */

public class TasksFragment extends Fragment {
    private static final String TAG = "TasksFragment";

    Intent intent;
    private RelativeLayout layoutToAdd;


//    public void onCreate (Bundle savedInstanceState) {
    // super.onCreate(savedInstanceState);
    //}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
        //View rootView = inflater.inflate(R.layout.fragment_tasks, null);
        //layoutToAdd = (RelativeLayout) findViewById(R.id.TaskFragment);

        Button ID = (Button) rootView.findViewById(R.id.btn_create_appraisal_rpt);
        ID.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AppraisalReportFragment appraisalRptAct = new AppraisalReportFragment();

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


                //fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragmentTransaction.addToBackStack(null);

                //Fragment newFragment = new AppCompatDialogFragment();
                //FragmentTransaction transaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.tasks_page_fragment, appraisalRptAct);

                //transaction.replace(R.id.child_fragment, newFragment);
                fragmentTransaction.commit();
            }
        });

        Button ID2 = (Button) rootView.findViewById(R.id.btn_check_in_for_today);
        ID2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                TasksFragment checkInForToday = new TasksFragment();

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.herds_check_in_for_today_fragment4, checkInForToday);

                //fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                //fragmentTransaction.addToBackStack(null);

                //Fragment newFragment = new AppCompatDialogFragment();
                //FragmentTransaction transaction = getFragmentManager().beginTransaction();

                //transaction.replace(R.id.child_fragment, newFragment);
                fragmentTransaction.commit();
            }
        });

        /*rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(getActivity(), AppraisalReportActivity.class);
                final Button button = (Button) rootView.findViewById(R.id.button_create_appraisal_rpt);
            }
        });*/

        return rootView;
    }

    /*public void buttonOnClick(View v){

    }*/
}

fragment_tasks.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:id="@+id/tasks_page_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <!-- First A -->
    <Button
        android:id="@+id/btn_check_in_for_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="27dp"
        android:layout_marginTop="44dp"
        android:background="@android:color/black"
        android:elevation="0dp"
        android:onClick="onCreateView"
        android:text="@string/btn_check_in_for_today"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <!-- First B -->
    <Button
        android:id="@+id/btn_create_appraisal_rpt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/btn_check_in_for_today"
        android:layout_below="@+id/btn_check_in_for_today"
        android:layout_marginTop="40dp"
        android:background="@android:color/black"
        android:onClick="onCreateView"
        android:text="@string/btn_create_appraisal_report"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <!-- First B-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView3"
        android:layout_below="@+id/btn_check_in_for_today"
        android:text="@string/tasks_check_day_score"
        android:textColor="@android:color/white"
        android:id="@+id/textView2" />

    <!-- First B-->
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_create_appraisal_rpt"
        android:layout_centerHorizontal="true"
        android:text="@string/tasks_print_email_regmail"
        android:textColor="@android:color/white" />


</RelativeLayout>


**AppraisalReportFragment**

package usjersey.com.jerseyscore.fragments;

import android.app.Activity;
//import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import usjersey.com.jerseyscore.R;

/**
 * Created by dhiggins on 11/29/2017.
 */

public class AppraisalReportFragment extends Fragment {
        private static final String TAG = "Appraisal Report";
        TextView textView;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                //return inflater.inflate(R.layout.fragment_information, container, false);
                View view = inflater.inflate(R.layout.fragment_appraisal_report, container, false);
                TextView output = (TextView)view.findViewById(R.id.textView3);
                //output.setText("Frag 2");
                return view;
        }
}

fragment_appraisal.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:id="@+id/tasks_page_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_check_in_for_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/appraisal_report"
        android:layout_marginStart="27dp"
        android:layout_marginTop="19dp"
        android:background="@android:color/black"
        android:text="@string/btn_all_cows_in_herd"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_cows_scored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/btn_check_in_for_today"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="23dp"
        android:background="@android:color/black"
        android:onClick="onCreateView"
        android:text="@string/btn_cows_scored"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView3"
        android:layout_below="@+id/btn_check_in_for_today"
        android:text="@string/label_all_cows_in_herd_report"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_cows_scored"
        android:layout_centerHorizontal="true"
        android:text="@string/label_checked_scored_cows"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/appraisal_report"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:fontFamily="serif"
        android:text="@string/title_appraisal_reports"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

Solution

  • When you inflate your fragment you should setVisibility to gone to your buttons from the previous layout after committing the FragmentManager's transaction in your activity

    You can do it like this and then change them to visible again when returning

    b1.setVisibility(View.GONE);
    b2.setVisibility(View.GONE);
    

    Change the names of the buttons according to your code