0

I am trying to replace the fragment inside a fragment, when i try to do this the fragment which i am trying to replace is getting added below to existing one instead of replacing it. I have tried all the suggestions and answers on stack overflow but every effort went in vain.

Here is a code.

Comments.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAppBG"
    android:layoutDirection="locale"
    android:textAlignment="center"
    android:textDirection="locale">

    <LinearLayout
        android:id="@+id/handover_comments_heading"
        android:layout_width="match_parent"
        android:layout_height="74dp"
        android:layout_below="@id/toolbar"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingStart="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="@string/comments"
            android:textColor="#FFFFFF"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/handover_comments_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/handover_comments_heading"
        android:orientation="vertical"
        android:paddingEnd="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingStart="10dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <EditText
                android:id="@+id/handover_comments_comments"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:gravity="start"
                android:hint="@string/enter_your_comments"
                android:maxLength="320"
                android:textSize="20sp" />

        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/handover_comments_textcounter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="end"
            android:paddingEnd="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingStart="10dp"
            android:text="0/320" />

        <CheckBox
            android:id="@+id/handover_comments_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="70dp"
            android:text="Check text"
            android:textColor="@color/colorAppTextColor"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">

            <Button
                android:id="@+id/handover_comments_save"
                android:layout_width="150dp"
                android:layout_height="70dp"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:background="#EC3E45"
                android:text="@string/save"
                android:textColor="#FFFFFF"
                android:textSize="20sp" />

            <Button
                android:id="@+id/handover_comments_cancel"
                android:layout_width="150dp"
                android:layout_height="70dp"
                android:background="#3B424C"
                android:text="@string/cancel"
                android:textColor="#FFFFFF"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>

    <FrameLayout
        android:layout_marginTop="10dp"
        android:background="@color/colorAppBG"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/handover_comments_layout">

        <fragment
            android:id="@+id/handover_sign_off_base_fragment"
            android:name="com.oviyum.fleetfoot.mvp.handover.sign_off.OTPFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_otp" />
    </FrameLayout>
</RelativeLayout>

Signature.xml

<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorAppBG"
    tools:context="com.oviyum.fleetfoot.mvp.handover.sign_off.SignatureFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="74dp"
                android:background="@color/colorAccent"
                android:orientation="horizontal"
                android:paddingEnd="10dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingStart="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:text="Signature"
                    android:textColor="#FFFFFF"
                    android:textSize="25sp" />
            </LinearLayout>

            <com.github.gcacace.signaturepad.views.SignaturePad
                android:id="@+id/handover_signature_signature_pad"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="#FFFFFF"
                app:penColor="@android:color/black" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="right"
                android:paddingEnd="10dp"
                android:paddingRight="10dp">

                <Button
                    android:id="@+id/handover_signature_upload_signature"
                    android:layout_width="wrap_content"
                    android:layout_height="70dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#EC3E45"
                    android:padding="10dp"
                    android:text="Upload Signature"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp" />

                <Button
                    android:id="@+id/handover_signature_resend_otp"
                    android:layout_width="150dp"
                    android:layout_height="70dp"
                    android:background="#3B424C"
                    android:text="RESEND OTP"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp" />
            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:text="RATE YOUR EXPERIENCE"
                android:textSize="20sp" />

            <RatingBar
                android:id="@+id/handover_signature_experience"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="50dp"
                android:numStars="5"
                android:scaleX="2"
                android:scaleY="2" />

            <Button
                android:id="@+id/handover_signature_submit"
                android:layout_width="150dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:layout_marginTop="50dp"
                android:background="#EC3E45"
                android:text="SUBMIT"
                android:textColor="#FFFFFF"
                android:textSize="20sp" />

        </LinearLayout>
    </ScrollView>


</LinearLayout>

OTPFragment.Java

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    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.EditText;
    import com.oviyum.fleetfoot.R;
    import com.oviyum.fleetfoot.others.ToastHelper;

    public class OTPFragment extends Fragment implements View.OnClickListener {

    private View view;
    private Activity activity;
    private Context context;

    private Button submitButton;
    private Button resendButton;

    private EditText enterOTP;


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


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

    public void init(){
        submitButton = (Button)view.findViewById(R.id.handover_otp_submit);
        submitButton.setOnClickListener(this);

        resendButton = (Button)view.findViewById(R.id.handover_otp_resend_otp);
        resendButton.setOnClickListener(this);

        enterOTP = (EditText)view.findViewById(R.id.handover_otp_enter_otp);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        this.activity = getActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.handover_otp_submit:
                ToastHelper.makeToast("clicked");
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                SignatureFragment llf = new SignatureFragment();
                ft.replace(R.id.handover_sign_off_base_fragment, llf);
                ft.addToBackStack(null);
                ft.commit();
                break;
            case R.id.handover_otp_resend_otp:
                break;
        }
    }
}

SignatureFrament.Java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.oviyum.fleetfoot.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class SignatureFragment extends Fragment implements View.OnClickListener {
    private Activity activity;
    private Context context;

    private View view;

    private SignaturePad signaturePad;
    private Button uploadSignature;
    private Button resendOTP;
    private Button submit;

    private RatingBar ratingBar;

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


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

    private void init() {
        signaturePad = (SignaturePad) view.findViewById(R.id.handover_signature_signature_pad);

        uploadSignature = (Button) view.findViewById(R.id.handover_signature_upload_signature);
        uploadSignature.setOnClickListener(this);

        resendOTP = (Button) view.findViewById(R.id.handover_signature_resend_otp);
        resendOTP.setOnClickListener(this);

        ratingBar = (RatingBar) view.findViewById(R.id.handover_signature_experience);

        submit = (Button) view.findViewById(R.id.handover_signature_submit);
        submit.setOnClickListener(this);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        this.activity = getActivity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.handover_signature_upload_signature:
                break;
            case R.id.handover_signature_resend_otp:
                break;
            case R.id.handover_signature_submit:
                break;
        }
    }
}
VINAY DANARADDI
  • 195
  • 2
  • 12
  • I even tried, getFragmentManager too, switched scroll layout directly into fragment, tweaked with width & height, etc – VINAY DANARADDI Oct 22 '16 at 12:09
  • `Fragment`s that are defined in your layout cannot be removed/replaced at runtime. Instead, transact it dynamically into the `FrameLayout` at startup, then your later `replace()` transaction will work as expected. – Mike M. Oct 22 '16 at 12:15
  • can u redirect me to some example? – VINAY DANARADDI Oct 22 '16 at 12:26
  • Well, you've got examples of everything you need right in your code. Just remove the `` from your layout, and in the `Activity`'s `onCreate()` method, perform a `FragmentTransaction` for the `OTPFragment`, just like you're doing for `SignatureFragment`. Make sure both transactions use `R.id.handover_comments_layout`. – Mike M. Oct 22 '16 at 12:34
  • 1
    Thanks a lot Mike, Thing got worked! – VINAY DANARADDI Oct 22 '16 at 12:36

0 Answers0