fragment on android

1

Hi, I am using fragment in android and the problem is that from a button that has an activity it calls a fragment but the views of the activity overlap in the fragment

this is the activity

and when I pulse in the floatingbutton it calls the fragment but it is behind the views of that activity

this is the onClick method of the floating button

@Override
    public void onClick(View view) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = 
        fragmentManager.beginTransaction();
        Dates fragment = new Dates();
        //secondA es el id del FrameLayout
        transaction.add(R.id.secondA,fragment);
        transaction.commit();

    }

this is the fragment onCreateView method

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View view = inflater.inflate(R.layout.dates_inputs, container,false); 

    return view;
}

activity layout:

<android.support.design.widget.CoordinatorLayout 
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:fitsSystemWindows="true"
tools:context="practica.myapplication.SecondActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/secondA">

</FrameLayout>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:background="@color/colorAccent"
    android:src="@drawable/add" />


<Button
    android:text="Boton de la activity"
    android:id="@+id/fragmentir"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>

fragment layout:

<FrameLayout 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"
tools:context="practica.myapplication.Dates"
android:id="@+id/frame"
android:background="@color/transparent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorText"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Datos"
        android:layout_gravity="center"
        android:layout_margin="15dp"
        android:textSize="20dp"/>


    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/fecha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="text"
            android:hint="Fecha"
            android:drawablePadding="7dp"
            android:drawableLeft="@drawable/calendario"/>

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

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/trabajoR"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:drawableLeft="@drawable/trabajo"
            android:inputType="text"
            android:hint="Trabajo realizado"
            android:drawablePadding="7dp"
            android:layout_gravity="center"/>
    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/cliente"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Cliente"
            android:drawableLeft="@drawable/cliente"
            android:drawablePadding="7dp"/>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/cancelar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancelar"
            android:background="@color/colorText"
            android:textColor="@color/colorAccent"
            android:layout_margin="20dp"/>

        <Button
            android:id="@+id/aceptar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aceptar"
            android:background="@color/colorAccent"
            android:textColor="@color/colorText"
            android:layout_margin="20dp"/>


    </LinearLayout>
</LinearLayout>
</FrameLayout>

is there any way that the views of the activity are behind the fragment?

    
asked by Lucho Juniors 02.12.2017 в 19:19
source

2 answers

0

Here the problem is that you are adding the Fragment within your main Layout of the activity:

transaction.add(R.id.secondA,fragment);

Which is incorrect, for that reason the elements overlap:

<android.support.design.widget.CoordinatorLayout 
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:id="@+id/secondA"
android:fitsSystemWindows="true"
tools:context="practica.myapplication.SecondActivity">

To solve this, consider adding a FrameLayout in your .xml and that's where the content of your Fragment would be replaced.

Adding the Framelayout indicates only change the Id to perform the Fragment transaction:

@Override
    public void onClick(View view) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = 
        fragmentManager.beginTransaction();
        Dates fragment = new Dates();

        //* Define id del framelayout.
        transaction.add(R.id.?????,fragment);
        transaction.commit();

    }
    
answered by 02.12.2017 / 19:50
source
1

Ok, the problem is that you should not use the CoordinatorLayout as fragment container, use FrameLayout in your place, so the Layout of your Activity would look like this:

<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:layout_height="match_parent"
android:id="@+id/secondA"
android:fitsSystemWindows="true"
tools:context="practica.myapplication.SecondActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:background="@color/colorAccent"
    android:src="@drawable/add" />


<Button
    android:text="Boton de la activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"/>

</FrameLayout>

Try it and tell us

    
answered by 02.12.2017 в 19:51