Fragment is not replacing the marked layout

3

I'm working on a college project and I'm having a hard time replacing a given layout with a fragment. What is happening is that the snippet is not replacing the layout with activity but rather, merging with it.

This is the block of code that calls the fragment:

    botaoDeFalar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            String textoParaFragment = editTextPrincipal.getText().toString();
            bundle.putString("oqSeraFalado", textoParaFragment);
            TesteFragment testefragment = new TesteFragment();
            testefragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction().replace(R.id.LayoutMain, testefragment).commit();
            oqSeraFalado = textoParaFragment;
            vamosFalar();
        }
    });
}

The variable BotaoDeFalar is a layout element of the MainActivity (button) which, when pressed, executes this entire block.

The ID LayoutMain, is the layout that contemplates all other elements of the Activity.

<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.mikha.projetointegrador5android.MainActivity"
    android:id="@+id/LayoutMain">

This is the Java of the fragment in question:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_teste, container, false);
    resultadoPalavra = (TextView) view.findViewById(R.id.TextViewTesteFragment);
    palavraDigitada = this.getArguments().getString("oqSeraFalado");
    resultadoPalavra.setText(palavraDigitada);
    return view;

}

The result of this when I run the application is that instead of fragment replacing everything we see on the main screen with the fragment XML, it adds the fragment with the XML of the main screen, as if merging.

    
asked by anonymous 31.03.2017 / 17:26

1 answer

0

It is not possible to replace the layout of the activity with that of the fragment.

In terms of layout, what the replace() method does is to replace the layout of a fragment, previously added to the given view group, by the latter. If no fragment has been added to the given group, the method adds this and includes its layout in the view group.

So that when adding a fragment your layout is not merged with the other content it must be added to an empty group view included in the activity layout, usually a FrameLayout.

So, if you want to replace all the contents of the activity layout, this layout should only have one FrameLayout with nothing inside, then use two fragments, which you already have and another one for the content that had activity.

    
31.03.2017 / 17:54