Here lads, I've honestly been trying to do this every which way but for some reason the different fragments just won't show up properly. I've gotten as far to the point where a fragment shows up but its on top of the home fragment. I'm so frustrated. I've been at this for DAYS
Here's the important part of the MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
// 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_home, R.id.nav_members, R.id.nav_map,
R.id.nav_news, R.id.nav_livestream, R.id.nav_election,
R.id.nav_share, R.id.nav_login)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.bringToFront(); */
// navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
Toast.makeText(getApplicationContext(), "Home is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
break;
case R.id.nav_members:
Toast.makeText(getApplicationContext(), "Members are selected",Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new MembersFragment()).commit();
break;
case R.id.nav_map:
Toast.makeText(getApplicationContext(), "Map is selected", Toast.LENGTH_LONG).show();
FragmentManager mapfm= getSupportFragmentManager();
MapFragment mapFragment = new MapFragment();
mapfm.beginTransaction().replace(R.id.drawer_layout, mapFragment).commit();
break;
case R.id.nav_news:
Toast.makeText(getApplicationContext(), "News is selected", Toast.LENGTH_LONG).show();
Intent newsStart =new Intent(MainActivity.this, MainActivityGaeilge.class);
startActivity(newsStart);
break;
case R.id.nav_livestream:
Toast.makeText(getApplicationContext(), "Livestream is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new LivestreamFragment()).commit();
break;
case R.id.nav_election:
Toast.makeText(getApplicationContext(), "Election info is selected", Toast.LENGTH_LONG).show();
//Intent electionopen = new Intent(MainActivity.this, ElectionFragment.class);
// startActivity(electionopen);
FragmentManager electionfm= getSupportFragmentManager();
ElectionFragment electionFragment = new ElectionFragment();
electionfm.beginTransaction().replace(R.id.home_page, electionFragment).commit();
break;
case R.id.nav_share:
Toast.makeText(getApplicationContext(), "Share is selected", Toast.LENGTH_LONG).show();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new SendFragment()).commit();
//Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
//Intent sharestart=new Intent(MainActivity.this,this.class);
// startActivity(sharestart);
break;
case R.id.nav_login:
Toast.makeText(getApplicationContext(), "Login is selected", Toast.LENGTH_LONG).show();
break;
// case R.id.nav_election:
// Intent electionopen = new Intent(MainActivity.this, ElectionFragment.class);
// startActivity(electionopen);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Here's the Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:context=".MainActivity"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
Here's the app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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/home_page"
tools:context=".MainActivity"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.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" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here's the content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
<TextView
android:id="#+id/textView"
android:layout_width="56dp"
android:layout_height="27dp"
android:layout_marginStart="82dp"
android:layout_marginTop="227dp"
android:layout_marginEnd="273dp"
android:layout_marginBottom="477dp"
android:text="Text: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="218dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="466dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="138dp"
android:layout_marginTop="328dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="355dp"
android:onClick="OnLogin"
android:text="Login Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="274dp"
android:layout_marginEnd="265dp"
android:layout_marginBottom="438dp"
android:onClick="OnLogin"
android:text="Password:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="169dp"
android:layout_marginTop="268dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="418dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
PLEASE HELP IM SO STRESSED
Currently you have two containers into which a Fragment might go:
You use the support FragmentManager all over your MainActivity with a FrameLayout with android:id="#+id/fragment_container" to add and replace various Fragments
You seem to have set up a navigation graph, at least there is a NavHostFragment in content_main.xml and some code related to NavController in comments in MainActivity
It looks like you add HomeFragment twice: once as start Fragment in the navigation graph and the other time in onCreate() via the support FragentManager
So when you're navigating through your app, you see the Fragments displayed on top of the HomeFragment instance in the NavHostFragment
Solution: use the support FragmentManager or the NavController consistently throughout your app and remove the container which belongs to the other approach (either the NavHostFragment or the FrameLayout)
The following suggestions assume you want to use FragmentManager:
Remove the FrameLayout serving as container for the Fragments from activity_main.xml:
<androidx.drawerlayout.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:context=".MainActivity"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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" />
</androidx.drawerlayout.widget.DrawerLayout>
... and add it to content_main.xml in place of the NavHostFragment
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/textView"
android:layout_width="56dp"
android:layout_height="27dp"
android:layout_marginStart="82dp"
android:layout_marginTop="227dp"
android:layout_marginEnd="273dp"
android:layout_marginBottom="477dp"
android:text="Text: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="218dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="466dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="138dp"
android:layout_marginTop="328dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="355dp"
android:onClick="OnLogin"
android:text="Login Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="274dp"
android:layout_marginEnd="265dp"
android:layout_marginBottom="438dp"
android:onClick="OnLogin"
android:text="Password:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="169dp"
android:layout_marginTop="268dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="418dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Please note that you can delete the file res/navigation/mobile-navigation.xml since it was only used by the NavHostFragment
Related
I have a HomeActivity which initiates the ActionBarDrawerToggle as follows:
private fun initDrawer() {
val toolbar = binding.fragmentHomeContainerView.findViewById<MaterialToolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
drawerToggle = ActionBarDrawerToggle(
this,
binding.drawer,
toolbar,
R.string.open_drawer,
R.string.close_drawer
)
binding.drawer.addDrawerListener(drawerToggle)
drawerToggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
binding.navigationView.setNavigationItemSelectedListener {
handleNavigationItemSelected(it)
true
}
}
The toolbar in question is inside a Fragment. Everything's normal and working until here, the toolbar is like Gmail's search with the hamburguer icon. But when I navigate to another fragment (that doesn't have this toolbar, but instead has an arrow back button), and then navigate back again to the first fragment (the one that has the toolbar) the icon doesn't work anymore. What's the best/correct solution for this? I've tried moving the initDrawer() function within the fragment but the application frozes when creating the ActionBarDrawerToggle I assume because of the activity passed to the ActionBarDrawerToggle
drawerToggle = ActionBarDrawerToggle(
activity = (requireActivity() as HomeActivity),
...
)
Moving the toolbar from the Fragment to the Activity isn't a solution neither, not because it doesn't fix the problem (which I'm not sure) but because I need the toolbar inside the fragment.
Minimal reproducible code:
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
private lateinit var drawerToggle: ActionBarDrawerToggle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
initDrawer()
}
private fun initDrawer() {
val toolbar = binding.fragmentContainerView.findViewById<MaterialToolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
drawerToggle = ActionBarDrawerToggle(
this,
binding.drawer,
toolbar,
R.string.app_name,
R.string.app_name
)
binding.drawer.addDrawerListener(drawerToggle)
drawerToggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_home" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
android:visibility="visible" />
</androidx.drawerlayout.widget.DrawerLayout>
Fragment1.kt:
class Fragment1 : Fragment(R.layout.fragment_1) {
private val binding: Fragment1Binding by lazy {
Fragment1Binding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {
navigateToFragment2()
}
}
private fun navigateToFragment2() {
val action = R.id.navigateToFragment2
Navigation.findNavController(requireView()).navigate(action)
}
}
fragment_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/entity_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment1">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
android:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:liftOnScroll="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_margin="6dp"
android:background="#drawable/rounded_toolbar"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="#drawable/ic_baseline_menu_24"
app:titleTextColor="#android:color/white">
<EditText
android:id="#+id/entities_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="Search for client"
android:background="#android:color/transparent"
android:hint="search"
android:inputType="textPersonName"
android:minHeight="48dp"
android:textColorHint="#color/black" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate to fragment 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBarLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment2.kt:
class Fragment2 : Fragment(R.layout.fragment_2) {
private val binding: Fragment2Binding by lazy {
Fragment2Binding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setActionBarAction()
}
private fun setActionBarAction() {
val action = R.id.navigateToFragment1
binding.toolbar.setOnClickListener {
Navigation.findNavController(it).navigate(action)
}
}
}
fragment_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment2">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#android:color/transparent"
android:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:liftOnScroll="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|start"
android:clickable="true"
android:focusable="true"
android:minHeight="?attr/actionBarSize"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/ic_baseline_chevron_left_32"
app:titleTextColor="#android:color/white">
</com.google.android.material.appbar.MaterialToolbar>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/app_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate back with the arrow button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<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/nav_home"
app:startDestination="#id/fragment1">
<fragment
android:id="#+id/fragment1"
android:name="com.example.myapplication.Fragment1"
android:label="fragment_1"
tools:layout="#layout/fragment_1" >
<action
android:id="#+id/navigateToFragment2"
app:destination="#id/fragment2" />
</fragment>
<fragment
android:id="#+id/fragment2"
android:name="com.example.myapplication.Fragment2"
android:label="fragment_2"
tools:layout="#layout/fragment_2" >
<action
android:id="#+id/navigateToFragment1"
app:destination="#id/fragment1" />
</fragment>
</navigation>
I make an application android for a school project , and actually i have a problem.
I have create 2 activity (MainActivity and BottomNavActivity), and I try to navigate between them, but the "fragment" home page in my navigation Remain undefined.
I found this tutorial in order to navigate between the fragments
and I added events) :
https://medium.com/#oluwabukunmi.aluko/bottom-navigation-view-with-fragments-a074bfd08711
But, now, I can navigate between my "fragments" BUT my initial "fragment" (the one that appears first when launching the activity), remains, and therefore when changing fragments, it is duplicated.
Here is a video of my problem
My Infos.class
public class Infos extends AppCompatActivity {
private ActivityInfosBinding binding;
//I create my variable
final Fragment frag2 = new ProduitsFragment();
final Fragment frag1 = new EntrepriseFragment();
final Fragment frag3 = new CGUFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = frag1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityInfosBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
//I add my differents fragments
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos, frag3).hide(frag3).commit();
fm.beginTransaction().add(R.id.nav_host_fragment_activity_infos,frag1).hide(frag1).commit();
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull #NotNull MenuItem item) {
switch (item.getItemId()) {
//My first fragment
case R.id.navigation_entreprise:
fm.beginTransaction().hide(active).show(frag1).commit();
active = frag1;
return true;
//My second fragment (default in video)
case R.id.navigation_CGU:
fm.beginTransaction().hide(active).show(frag3).commit();
active = frag3;
return true;
}
return false;
}
});
}
}
And my mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<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/navigation_CGU"> //I try to change this, but nothing. This fragment is duplicated.
<fragment
android:id="#+id/navigation_entreprise"
android:name="fr.romaindrouhot.aurelinfo.ui.home.EntrepriseFragment"
android:label="#string/nav_entreprise"
tools:layout="#layout/fragment_entreprise" />
<fragment
android:id="#+id/navigation_produits"
android:name="fr.romaindrouhot.aurelinfo.ui.home.ProduitsFragment"
android:label="#string/nav_produits"
tools:layout="#layout/fragment_produits" />
<fragment
android:id="#+id/navigation_CGU"
android:name="fr.romaindrouhot.aurelinfo.ui.home.CGUFragment"
android:label="#string/nav_CGU"
tools:layout="#layout/fragment_cgu" />
</navigation>
Can you help me ?
yes, here is my activity_infos.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
My fragment_cgu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="50sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
My fragment_entreprise.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.home.EntrepriseFragment"
android:id="#+id/fragment_entreprise"
android:background="#drawable/logonoiretblanc">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/entreprise_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textColor="#color/black"
android:textSize="#dimen/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/border"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/entreprise_title"
android:layout_marginTop="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/informations"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</LinearLayout>
<TextView
android:id="#+id/title_gerant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gerant_title"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
android:layout_marginTop="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/gerant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gerant"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_gerant"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_localisation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/localisation_title"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/gerant"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/localisation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/localisation"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_contact"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_activite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_activite"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
app:layout_constraintTop_toBottomOf="#+id/localisation"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/activite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/activite"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_activite"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/title_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_contact"
android:textAlignment="center"
android:textColor="#color/dark_green"
android:shadowColor="#color/black"
android:shadowRadius="1"
android:textSize="#dimen/gerant_title"
android:layout_marginTop="#dimen/espace_entre_text_title"
app:layout_constraintTop_toBottomOf="#+id/activite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/contact_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact"
android:textAlignment="center"
android:textColor="#color/blue"
android:textSize="#dimen/gerant"
app:layout_constraintTop_toBottomOf="#+id/title_contact"
android:layout_marginTop="#dimen/espace_entre_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
As I mentioned in the comment section, the Navigation Component deals itself with the transactions between the fragments.
Here some tips to set up your BottomNavigation bar, hope it will help you.
First of all your mobile_navigation.xml and fragment XML seems to be good.
I could fix a bit your activity_infos.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="Odp"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:navGraph="#navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
...
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Update your Left/Right constraint with Start/End here is why
Add a constraint between the bottom of the id/nav_host_fragment_activity_infos and the id/nav_view, just to be sure the fragment never be hidden by the navbar. It is optional.
Then, your activity could be updated without the FrragmentManager:
public class Infos extends AppCompatActivity {
private ActivityInfosBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// When you are using an activity you can directly use this
binding = DataBindingUtil.setContentView(this, R.layout.activity_infos);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top-level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_entreprise, R.id.navigation_produits, R.id.navigation_CGU)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_infos);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
}
}
I found this tutorial, it explains step by step, and it may provide more info than my "tips".
Can you please try replacing this with your activity_infos.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_infos"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried to implement navigation drawer in my app with androidx library. So far, the app show hamburger icon on top left corner, but to access the drawer I have to swipe right. If I clicked the icon it will go to previous activity instead. Is it because this activity is not the main activity? How can I fix this? Thank you in advance.
Here is my activity
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
NavigationView nv = findViewById(R.id.navigationView);
nv.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
switch(id)
{
case R.id.action_open_list:
break;
case R.id.action_closed_list:
Intent closedListIntent = new Intent(this, ClosedListActivity.class);
startActivity(closedListIntent);
break;
default:
return true;
}
return true;
});
}
my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Toolbar needs to be a child of DrawerLayout to work with.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.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" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
android:id="#+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Then in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
....
}
Good Day everyone, I using the left navigation bar and i use fragment for the activity in the navigation bar. But the problem is once I click on the activity, the fragment could not show up. I am not sure what is the problem.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initCollapsingToolbar();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
informationList = new ArrayList<>();
adapter = new AlbumsAdapter(this, informationList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,
2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,
dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
//Cover Pic
try {
Glide.with(this).load(R.drawable.project1_cover).into((ImageView)
findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
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);
setupDrawerContent(navigationView);
}
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(getApplicationContext(), "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(getApplicationContext(), "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
fragment = fragment_manageProfile.class;
break;
case R.id.logout:
break;
default:
break;
}
try{
myFragment = (Fragment)fragment.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayout,
myFragment).addToBackStack(null).commit();
menuItem.setChecked(true);
drawer.closeDrawers();
}
content_main xml File
<?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:background="#color/viewBg"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.kopilim.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
app_bar_main xml file
<?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=".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" />
</android.support.design.widget.CoordinatorLayout>
activity_main xml file
<?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"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frameLayout"
android:background="#drawable/login_background">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
PS fragmentLayout is currently in my activity_main.xml file there. Because I not sure where it has to be. For now, everything i mean the view of my content, my layout is fined. But just for the activity in the navigation bar, fragment there is not showing.
Where did I make my mistake?
Edited: My fragment Class
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="info.androidhive.kopilim.fragment_manageProfile">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="35dp"
android:textSize="22dp"
android:textColor="#10019f"
android:textStyle="bold"
android:text="#string/manageProfile"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empEmail"
android:text="#string/email"
android:layout_margin="10dp"
android:textSize="18dp"
android:gravity="center_vertical"
android:textColor="#color/colorAccent"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empID"
android:hint="#string/profileID"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empName"
android:hint="#string/profileName"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empIC"
android:hint="#string/profileIC"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/male"
android:id="#+id/maleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/female"
android:id="#+id/femaleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"/>
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empPh"
android:hint="#string/profileHp"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empAddress"
android:hint="#string/profileAddress"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/profileBtn"
android:text="#string/updateProfileBtn"
android:background="#color/buttonLogin"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
fragment java class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_manage_profile, container,
false);
....
....
//my activity
return v;
}
FrameLayout is ok in your activity layout file because you want to display the fragment on the same screen. About the fragment displaying . You need to have a custom fragment class to show it's view and to support it's business logic.
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(MainActivity.this, "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(MainActivity.this, "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
showMyFragment();
break;
case R.id.logout:
break;
default:
break;
}
}
public void showMyFragment(){
Fragment menuFragment = new MyCustomFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout, menuFragment).commit();
}
And here you have to declare the custom fragment class :
public class MyCustomFragment extends Fragment {
//this text view is got from the xml below
private TextView tvTest;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bindView(view);
//start your logic here
}
private void bindView(View view){
tvTest = view.findViewById(R.id.tv_fragment_test);
Toast.makeText(getContext(),tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
my_fragment_layout.xml
// could be any kind of layout
// for example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
android:id="#+id/tv_fragment_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I'm showing on a fragment"
android:src="#drawable/app_img_small" />
</LinearLayout>
I'm trying to create a sidebar navigation drawer that slides when a user clicks the button. I'm trying to add this feature into my existing layout and I'm having trouble with it. As you can see in my layout, I have created a menu resource file and included into my main layout but it is not working. Any help would be appreciated.
Here's my main layout
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
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:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="113dp"
android:fitsSystemWindows="true"
app:popupTheme="#style/Theme.AppCompat.DayNight.DarkActionBar"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textSize="14sp"
android:hint="#string/search_hint"
android:background="#drawable/bottom_border"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<include
layout="#layout/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeAd"
android:layout_below="#+id/search_box"
/>
<LinearLayout
android:id="#+id/relativeAd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/divider"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:layout_height="53dp"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="#drawable/menu_1"
android:id="#+id/floatingActionButton9"
app:backgroundTint="?attr/actionModeSplitBackground"
app:rippleColor="#android:color/holo_red_dark"
android:layout_gravity="bottom|left"
app:layout_anchor="#+id/frameLayout"
app:layout_anchorGravity="center_vertical|right" />
Here is my screenshot of my layout
My existing layout
Try this out.
This a code for navigation drawer which opens and closes using button and swipe both.
so below is the layout code for setting up navigation drawer.
xml file 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:theme="#style/NavigationTheme">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.whiskey.servicedog.TrainingProgrammes">
<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.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_navigation"
android:background="#drawable/nav_bg"
android:theme="#style/NavigationViewStyle"
app:itemTextColor="#color/White"
app:itemIconTint="#color/White"
/>
</android.support.v4.widget.DrawerLayout>
now java code for setting up navigation drawer.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id._aboutus:
Intent aboutusintent = new Intent(TrainingProgrammes.this, AboutUS.class);
aboutusintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aboutusintent);
break;
case R.id._trainingprog:
Toast.makeText(getApplicationContext(), "Training Programmes", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id._taskhis:
Intent historyintent = new Intent(TrainingProgrammes.this, TaskHistory.class);
historyintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(historyintent);
break;
case R.id.taskrem:
Intent newintent = new Intent(TrainingProgrammes.this, TaskReminders.class);
newintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newintent);
break;
case R.id.settingschange:
Intent settingIintent = new Intent(TrainingProgrammes.this, Settings.class);
settingIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(settingIintent);
break;
case R.id.help_btn:
Intent helpIintent = new Intent(TrainingProgrammes.this, Help.class);
helpIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(helpIintent);
break;
case R.id.logout:
Intent intent = new Intent(TrainingProgrammes.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
SharedPreferences preferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
if (preferences.contains("password")) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove("password");
editor.commit();
}
finish(); // Call once you redirect to another activity.
}
return true;
}
});
Hope this helps you out.
Here is the screenshot of above working code.
Use below xml to add a Drawer Layout and include your previous layout
<include layout="#layout/content_home_page" /> by replacing this
name it activity_main.xml
<?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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<!--Main Content Goes Here-->
<include layout="#layout/content_home_page" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/colorPrimary"
app:menu="#menu/navigation_menu"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_menu_header_bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/user_placeholder" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedBold
android:id="#+id/memberFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Mr. John Doe"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedRegular
android:id="#+id/memberEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="john.doe#mail.com"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your activity:
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
inside your button click listener click event,
call drawer.openDrawer(GravityCompat.START);