Advertisement
Guest User

Untitled

a guest
Feb 7th, 2021
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 5.44 KB | None | 0 0
  1. Main activity layout:
  2.  
  3.     <androidx.fragment.app.FragmentContainerView
  4.        android:id="@+id/nav_host_fragment_container"
  5.        xmlns:android="http://schemas.android.com/apk/res/android"
  6.        xmlns:tools="http://schemas.android.com/tools"
  7.        xmlns:app="http://schemas.android.com/apk/res-auto"
  8.        android:layout_width="match_parent"
  9.        android:layout_height="match_parent"
  10.        tools:context=".ui.main.MainActivity"
  11.        android:name="androidx.navigation.fragment.NavHostFragment"
  12.        app:defaultNavHost="true"
  13.        app:navGraph="@navigation/nav_graph" />
  14.  
  15. Nav_grav:
  16.  
  17.     <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  18.        xmlns:app="http://schemas.android.com/apk/res-auto"
  19.        xmlns:tools="http://schemas.android.com/tools"
  20.        android:id="@+id/nav_graph"
  21.        app:startDestination="@id/loginFragment">
  22.    
  23.         <fragment
  24.            android:id="@+id/loginFragment"
  25.            android:name="com.retroroots.vila.ui.login.LoginFragment"
  26.            android:label="fragment_login"
  27.            tools:layout="@layout/fragment_login" >
  28.    
  29.             <action
  30.                android:id="@+id/action_loginFragment_to_homeContainerFragment"
  31.                app:destination="@id/homeContainerFragment"
  32.                app:popUpTo="@id/loginFragment"
  33.                app:popUpToInclusive="true"/>
  34.         </fragment>
  35.         <fragment
  36.            android:id="@+id/homeContainerFragment"
  37.            android:name="com.retroroots.vila.ui.Home.HomeContainerFragment"
  38.            android:label="fragment_home_container"
  39.            tools:layout="@layout/fragment_home_container" />
  40.     </navigation>
  41.  
  42. Login fragment layout:
  43.  
  44.     <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  45.        xmlns:app="http://schemas.android.com/apk/res-auto"
  46.        xmlns:tools="http://schemas.android.com/tools"
  47.        android:layout_width="match_parent"
  48.        android:layout_height="match_parent"
  49.        android:background="@drawable/app_background"
  50.        android:paddingTop="100dp"
  51.        tools:context=".ui.login.LoginFragment"
  52.        android:animateLayoutChanges="true">
  53.    
  54.         <com.google.android.material.button.MaterialButton
  55.            android:id="@+id/loginBtn"
  56.            android:layout_width="wrap_content"
  57.            android:layout_height="wrap_content"/>
  58.    
  59.     </androidx.constraintlayout.widget.ConstraintLayout>
  60.  
  61. The above button runs this from the login fragment:
  62.  
  63.     NavDirections navDirections = LoginFragmentDirections.actionLoginFragmentToHomeContainerFragment();
  64.    
  65.                     NavHostFragment.findNavController(currentFragment).navigate(navDirections);
  66.  
  67. Home Fragment layout:
  68.  
  69.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  70.        xmlns:tools="http://schemas.android.com/tools"
  71.        android:layout_width="match_parent"
  72.        android:layout_height="match_parent"
  73.        xmlns:app="http://schemas.android.com/apk/res-auto"
  74.        tools:context=".ui.Home.HomeContainerFragment"
  75.        android:orientation="vertical"
  76.        android:id="@+id/homeLayout">
  77.    
  78.         <com.google.android.material.bottomnavigation.BottomNavigationView
  79.            android:id="@+id/bottomNavVw"
  80.            android:layout_width="match_parent"
  81.            android:layout_height="wrap_content"
  82.            app:menu="@menu/home_top_nav_menu" />
  83.    
  84.         <androidx.fragment.app.FragmentContainerView
  85.            android:id="@+id/homeNavHostFragmentContainer"
  86.            android:layout_width="match_parent"
  87.            android:layout_height="match_parent"
  88.            android:name="androidx.navigation.fragment.NavHostFragment"
  89.            android:padding="10dp"
  90.            app:navGraph="@navigation/home_nav_graph"/>
  91.    
  92.     </LinearLayout>
  93.  
  94. This runs from the home's fragment "onViewCreated":
  95.  
  96.     BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomNavVw);
  97.    
  98.             NavController navController = NavHostFragment.findNavController(this);
  99.    
  100.             NavigationUI.setupWithNavController(bottomNavigationView, navController);
  101.  
  102. Menu home:
  103.  
  104.     <menu xmlns:android="http://schemas.android.com/apk/res/android">
  105.         <item
  106.            android:id="@+id/pendingOrdersFragment"
  107.            android:title="@string/pendingOrdersTabTxt" />
  108.    
  109.         <item
  110.            android:id="@+id/historyFragment"
  111.            android:title="@string/historyTabTxt" />
  112.    
  113.     </menu>
  114.  
  115. pendingOrdersFragment & historyFragment layout: Just a single text view with the name of the fragment
  116.  
  117. Home nav graph:
  118.  
  119.    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  120.        xmlns:app="http://schemas.android.com/apk/res-auto"
  121.        xmlns:tools="http://schemas.android.com/tools"
  122.        android:id="@+id/home_nav_graph"
  123.        app:startDestination="@id/pendingOrdersFragment">
  124.    
  125.        <fragment
  126.            android:id="@+id/pendingOrdersFragment"
  127.            android:name="com.retroroots.vila.ui.Home.pending.PendingOrdersFragment"
  128.            android:label="fragment_pending_orders"
  129.            tools:layout="@layout/fragment_pending_orders" />
  130.        <fragment
  131.            android:id="@+id/historyFragment"
  132.            android:name="com.retroroots.vila.ui.Home.history.HistoryFragment"
  133.            android:label="fragment_history"
  134.            tools:layout="@layout/fragment_history" />
  135.    </navigation>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement