1
I’m using Fragments instead of Activity, only when I call the second Fragment with the replace method, it doesn’t completely overlap the previous Fragment, it’s on top, but the drawing of the other Fragment keeps appearing, as I do so it doesn’t happen ?
xml code from the first screen
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/primeiro_fragment"
android:name="br.com.demmy.demet.gerador.PrimeiroFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</FrameLayout>
Code of the first fragment.java
public class PrimeiroFragment extends Fragment {
View minha_view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
minha_view = inflater.inflate(R.layout.tela_principal, container, false);
return minha_view;
}
}
main screen code.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">
<LinearLayout
android:id="@+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/textView_hora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:text="texto1" />
<EditText
android:id="@+id/editText_hora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/oval"
android:padding="8dp"
android:layout_margin="8dp"
android:ems="4"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_marginTop="3dp"
android:layout_marginBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Texto 2" />
<EditText
android:id="@+id/texto_notification"
android:padding="14dp"
android:gravity="start"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/oval"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button"
style="@style/estilo_botao"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="Cadastrar" />
</LinearLayout>
</FrameLayout>
Code of the second fragment.java
public class SegundoFragment extends Fragment {
View minha_view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
minha_view = inflater.inflate(R.layout.segundo_layout, container, false);
return minha_view;
}
}
code of the second layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="138dp"
android:layout_marginStart="138dp"
android:layout_marginTop="196dp"
android:text="SEGUNDO LAYOUT" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/textView2"
android:layout_marginLeft="68dp"
android:layout_marginStart="68dp"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/button2"
android:layout_marginTop="16dp"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button3"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="60dp"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView2"
android:text="Button" />
</RelativeLayout>
code that is calling the Fragments
lista_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
android.app.FragmentManager fragmentManager = getFragmentManager();
if (position == 0){
fragmentManager.beginTransaction().replace(R.id.frame_layout, new PrimeiroFragment()).commit();
}else if(position == 1){
fragmentManager.beginTransaction().replace(R.id.frame_layout, new SegundoFragment()).commit();
}else if(position == 2){
fragmentManager.beginTransaction().replace(R.id.frame_layout, new TerceiroFragment()).commit();
}
}
});
If you put
android:background="@android:color/white"
in theRelativeLayout
of the second Fragment can solve this problem.– LeoSantana
It is but this way, the components of the previous screen still remain on the screen, only invisible
– Demetrius
is true, has this problem. If you also put
android:clickable="true"
in the layout of the second Fragment the components of the first Fragment will not be clickable at least.– LeoSantana
@That’s like "sweep the garbage under the rug".
– ramaral