3
I’m working on a college project and I’m having a hard time replacing a given layout by a fragment. What is happening is that, the fragment is not replacing the layout of Activity but yes, mixing with it.
This is the code block 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 an element of layout Mainactivity(button) Which To Tight, Runs All This Block.
ID Layoutmain, is the layout which includes all other elements of 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 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 replace everything we see on the main screen with XML’s Fragment, it adds the XML of Fragment with the main screen XML, as if merged.
Related When changing Fragment, the previous one is visible below the new one
– ramaral