Error Fragment-Activity Android Studio Button

1

I have an activity (main activity) with a Start button and a floating actin button (the +), this activity has a menu and each of the sections are fragments. The fact is that in all the fragments of this activity I get that button and I can not delete it. I tried to change the xml by FrameLayout but nothing. It is as if it had a transparent background, although it has a white background. I'm pretty new to this and I do not know what else to try. Any ideas?

Code xlm fragment:

<?xml version="1.0" encoding="utf-8"?>                        
<RelativeLayout 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"
android:background="@color/colorBlanco"
tools:context=".Delay">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_canviar_delay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme" />

<TextView
    android:id="@+id/canviar_Delay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/toolbar_canviar_delay"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="9dp"
    android:text="@string/canviar_delay"
    android:textColor="@color/colorBlanco"
    android:textSize="24sp" />

[...]
</RelativeLayout>

Code xml content main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="core.ds.TimeTracker.MainActivity"
tools:showIn="@layout/app_bar_main">


 <Button
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/start"
    tools:layout_editor_absoluteX="148dp"
    tools:layout_editor_absoluteY="231dp"/>
</RelativeLayout>

Code xml activity main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

xml code where the floating button is located:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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.AppBarLayout>

<include layout="@layout/content_main" />

<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"
    app:backgroundTint="@color/colorAccent"
    app:srcCompat="@android:drawable/ic_menu_add" />

Java code of the buttons in the activity main:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openCrearPT();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);


    botonStart = (Button) findViewById(R.id.start);
    botonStart.setOnClickListener(botonListener);
}

 public void openCrearPT() {
    Intent intent = new Intent(this, CrearPT.class);
    startActivity(intent);
}

Code to pass between fragments:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    Fragment miFragment=null;
    boolean fragmentSeleccionado=false;

    if (id == R.id.gen_informe) {
        miFragment=new GenerarInforme();
        fragmentSeleccionado=true;
            } else if (id == R.id.delay) {
        miFragment=new Delay();
        fragmentSeleccionado=true;
    } else if (id == R.id.idioma) {
        miFragment=new Idioma();
        fragmentSeleccionado=true;
    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    if (fragmentSeleccionado) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_main,miFragment).commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
    
asked by maber03 01.01.2019 в 21:42
source

1 answer

0

Move the FAB to content main , since this layout is the one that is displayed when you launch the app ( activity main ):

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 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/content_main"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:context="core.ds.TimeTracker.MainActivity"
  tools:showIn="@layout/app_bar_main">

 <Button
 android:id="@+id/start"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="@string/start"
 tools:layout_editor_absoluteX="148dp"
 tools:layout_editor_absoluteY="231dp"/>

 <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"
 app:backgroundTint="@color/colorAccent"
 app:srcCompat="@android:drawable/ic_menu_add" />
 </RelativeLayout>

The FrameLayout , which is where the fragments will be displayed, should be within app_bar_main :

 <?xml version="1.0" encoding="utf-8"?>
 <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"
 tools:context="com.example.mench.glide01.MainActivity">

 <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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.AppBarLayout>

<include layout="@layout/content_main" />

<FrameLayout
    android:id="@+id/container"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

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

Therefore when calling the fragments, the container must be container and not how you have content_main.

And do not forget to add this property to the fragments xml:

 android:background="?android:colorBackground"
    
answered by 04.01.2019 в 01:02