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.