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 thing is that in all the fragments of this activity I get that button and I can't delete it. I have tried to change the xml for FrameLayout but nothing. It is as if it has a transparent background, although it has a white background set. I'm pretty new to this and I don't know what else to try. Any ideas?
xlm snippet code:
<?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>
Content main xml code:
<?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>
Activity main xml code:
<?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:
<?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 main activity:
@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;
}
Move the FAB to
content main
, since this layout is the one shown when launching the app (activity main
):The
FrameLayout
, which is where the snippets will be displayed, must be inside theapp_bar_main
:So when calling the fragments, the container must be
container
and not like you have content_main.And don't forget to add this property to the xml of the fragments: