I am using a navigation drawer, in it I have a button that I want to put a specific fragment in the "fragment container" but I do not know how to make it place, the tutorials that I have seen show in past versions and I do not understand how to use the navigation drawer .
This is the button: private Button bAdmin
, I want to put this fragment by pressing the button: AdminFragment.java
/ admin_fragment.xml
Fragments are controlled from mobile navigation:
<navigation 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/mobile_navigation"
app:startDestination="@id/nav_inicio">
<fragment
android:id="@+id/nav_inicio"
android:name="com.example.donmigue.ui.inicio.InicioFragment"
android:label="@string/menu_inicio"
tools:layout="@layout/fragment_inicio" />
<fragment
android:id="@+id/nav_comprar"
android:name="com.example.donmigue.ui.pedido.PedidoFragment"
android:label="@string/titulo_f_pedido"
tools:layout="@layout/pedido_fragment" />
<fragment
android:id="@+id/nav_menu_combos"
android:name="com.example.donmigue.ui.combos.CombosFragment"
android:label="@string/titulo_f_combo"
tools:layout="@layout/combos_fragment" />
<fragment
android:id="@+id/nav_menu_emp"
android:name="com.example.donmigue.ui.emp.EmpFragment"
android:label="@string/titulo_f_emp"
tools:layout="@layout/emp_fragment" />
<fragment
android:id="@+id/nav_menu_bebidas"
android:name="com.example.donmigue.ui.bebidas.BebidasFragment"
android:label="bebidas_fragment"
tools:layout="@layout/bebidas_fragment" />
<fragment
android:id="@+id/nav_cuenta_editar"
android:name="com.example.donmigue.ui.perfil.PerfilFragment"
android:label="@string/label_titulo_perfil"
tools:layout="@layout/perfil_fragment" />
<fragment
android:id="@+id/nav_cuenta_pedidos"
android:name="com.example.donmigue.ui.mPedidos.MisPedidosFragment"
android:label="mis_pedidos_fragment"
tools:layout="@layout/mis_pedidos_fragment" />
<fragment
android:id="@+id/a_ProductoFragment"
android:name="com.example.donmigue.ui.AProductos.A_ProductoFragment"
android:label="a__producto_fragment"
tools:layout="@layout/a__producto_fragment" />
<fragment
android:id="@+id/a_ClientesFragment"
android:name="com.example.donmigue.ui.AClientes.A_ClientesFragment"
android:label="Clientes"
tools:layout="@layout/a__clientes_fragment" />
<fragment
android:id="@+id/a_PedidosFragment"
android:name="com.example.donmigue.ui.APedidos.A_PedidosFragment"
android:label="a__pedidos_fragment"
tools:layout="@layout/a__pedidos_fragment" />
<fragment
android:id="@+id/AdminFragment"
android:name="com.example.donmigue.ui.Admin.AdminFragment"
android:label="ADMINISTRADOR"
tools:layout="@layout/admin_fragment" />
</navigation>
This is the code of the activity where I control in navigation drawer:
private FirebaseAuth mAuth;
private DatabaseReference fireData;
private AppBarConfiguration mAppBarConfiguration;
private TextView userName;
private Button bAdmin;
public static Activity PageA;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
PageA = this;
mAuth = FirebaseAuth.getInstance ();
fireData = FirebaseDatabase.getInstance (). getReference ();
setContentView (R.layout.activity_pagina);
Toolbar toolbar = findViewById (R.id.toolbar);
setSupportActionBar (toolbar);
DrawerLayout drawer = findViewById (R.id.drawer_layout);
NavigationView navigationView = findViewById (R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder (
R.id.nav_inicio, R.id.nav_comprar, R.id.nav_menu_combos, R.id.nav_menu_emp,
R.id.a_ClientesFragment, R.id.AdminFragment,
R.id.nav_cuenta_editar, R.id.nav_cuenta_pedidos, R.id.nav_menu_bebidas)
.setDrawerLayout (drawer)
.build ();
NavController navController = Navigation.findNavController (this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController (this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController (navigationView, navController);
navigationView.setItemIconTintList (null);
View hView = navigationView.getHeaderView (0);
userName = (TextView) hView.findViewById (R.id.navHeader_name);
bAdmin = (Button) hView.findViewById (R.id.btnAdmin);
getUserInfo ();
bAdmin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//PLACE THE FRAGMENT ADMIN
}
});
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater (). inflate (R.menu.page, menu);
return true;
}
//SIGN OFF
public void Close Session (View view) {
mAuth.signOut ();
Intent intent = new Intent (this, LoginActivity.class);
startActivity (intent);
finish ();
}
// GET AND PLACE USER INFORMATION
private void getUserInfo () {
String id = mAuth.getCurrentUser (). GetUid ();
fireData.child ("Person"). child (id) .addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot snapshot) {
if (snapshot.exists ()) {
String name = snapshot.child ("name"). GetValue (). ToString ();
userName.setText (name);
String value = snapshot.child ("value"). GetValue (). ToString ();
if (valor.equals ("1")) {
bAdmin.setVisibility (View.VISIBLE);
} else if (valor.equals ("0")) {
bAdmin.setVisibility (View.INVISIBLE);
}
}
}
@Override
public void onCancelled (@NonNull DatabaseError error) {
}
});
}
@Override
public boolean onSupportNavigateUp () {
NavController navController = Navigation.findNavController (this, R.id.nav_host_fragment);
return NavigationUI.navigateUp (navController, mAppBarConfiguration)
|| super.onSupportNavigateUp ();
}
@SuppressLint ("WrongConstant")
public void showSelectedFragment (Fragment fragment) {
getSupportFragmentManager (). beginTransaction (). replace (R.id.nav_controller_view_tag,
fragment)
.setTransition (FragmentTransaction.TRANSIT_ENTER_MASK)
.commit ();
}
@Override
public boolean onNavigationItemSelected (@NonNull MenuItem item) {
return false;
}