Giter Club home page Giter Club logo

navigation-component's Introduction

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Navigation Component Projects

This project contains 1 activity, 6 fragments, bottom naigation and navigation drawer ๐Ÿ‘ฝ

โญ๏ธ Watch YouTube Tutorial!

Preview ๐Ÿ‘€

๐ŸŒธ Bottom Navigation ๐ŸŒธ Navigation Drawer
๐ŸŒธ Options Menu ๐ŸŒธ Login Screen
  • nav_graph.xml

Screen Shot 2021-08-23 at 14 06 43

<?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_graph"
   app:startDestination="@id/homeFragment">

   <fragment
       android:id="@+id/homeFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.HomeFragment"
       android:label="Home"
       tools:layout="@layout/fragment_home">
       <action
           android:id="@+id/action_homeFragment_to_loginFragment"
           app:destination="@id/loginFragment"
           app:enterAnim="@anim/slide_in_right"
           app:exitAnim="@anim/slide_out_left"
           app:popEnterAnim="@anim/slide_in_left"
           app:popExitAnim="@anim/slide_out_right" />
   </fragment>
   <fragment
       android:id="@+id/loginFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.LoginFragment"
       android:label="Login"
       tools:layout="@layout/fragment_login">
       <action
           android:id="@+id/action_loginFragment_to_welcomeFragment"
           app:destination="@id/welcomeFragment"
           app:enterAnim="@anim/slide_in_right"
           app:exitAnim="@anim/slide_out_left"
           app:popEnterAnim="@anim/slide_in_left"
           app:popExitAnim="@anim/slide_out_right">
           <argument android:name="username" />
           <argument android:name="password" />
       </action>
       <argument
           android:name="username"
           android:defaultValue="@null"
           app:argType="string"
           app:nullable="true" />
       <deepLink
           android:id="@+id/deepLink"
           app:uri="example.com/login/{username}" />
   </fragment>
   <fragment
       android:id="@+id/welcomeFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.WelcomeFragment"
       android:label="{username}"
       tools:layout="@layout/fragment_welcome">
       <argument
           android:name="username"
           app:argType="string" />
       <argument
           android:name="password"
           app:argType="string" />
       <action
           android:id="@+id/action_welcomeFragment_to_homeFragment"
           app:destination="@id/homeFragment"
           app:enterAnim="@anim/slide_in_right"
           app:exitAnim="@anim/slide_out_left"
           app:popEnterAnim="@anim/slide_in_left"
           app:popExitAnim="@anim/slide_out_right"
           app:popUpTo="@id/homeFragment"
           app:popUpToInclusive="true" />
   </fragment>
   <fragment
       android:id="@+id/settingsFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.SettingsFragment"
       android:label="Settings"
       tools:layout="@layout/fragment_settings" />
   <fragment
       android:id="@+id/termsFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.TermsFragment"
       android:label="Terms &amp; Conditions"
       tools:layout="@layout/fragment_terms" />
   <action
       android:id="@+id/action_global_termsFragment"
       app:destination="@id/termsFragment"
       app:enterAnim="@anim/slide_in_top"
       app:exitAnim="@anim/slide_out_bottom"
       app:popEnterAnim="@anim/slide_in_bottom"
       app:popExitAnim="@anim/slide_out_top" />
   <fragment
       android:id="@+id/searchFragment"
       android:name="com.yagmurerdogan.navigationcomponent2.SearchFragment"
       android:label="Search"
       tools:layout="@layout/fragment_search" />
</navigation>
  • drawer_bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/ic_home"
        android:title="@string/home_screen" />

    <item
        android:id="@+id/searchFragment"
        android:icon="@drawable/ic_search"
        android:title="@string/search" />

</menu>
  • options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/termsAndConditions"
        android:title="Terms &amp; Conditions"
        app:showAsAction="never" />

    <item
        android:id="@+id/settingsFragment"
        android:menuCategory="secondary"
        android:title="@string/settings"
        app:showAsAction="never" />
</menu>

This project contains 2 fragments and 1 activity ๐Ÿ‘ฝ

โญ๏ธ Watch YouTube Tutorial!

Preview Video

Steps

  • Firstly, we should create Navigation Resource File. We can create this directory with using Resource Manager on Android Studio. My navigation resource file name is my_nav.xml.

  • After creating my_nav.xml file, add 2 fragment by selecting "New Destination" icon on my_nav.xml file. With this button we can create new destination easily.

Screen Shot 2021-08-18 at 16 30 34

  • my_nav.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/my_nav"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.hepsiburada.navigationcomponent.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" >
        <action
            android:id="@+id/navigateToSecondFragment"
            app:destination="@id/secondFragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim" />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.hepsiburada.navigationcomponent.SecondFragment"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" >
        <action
            android:id="@+id/navigateToFirstFragment"
            app:destination="@id/firstFragment"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"
            app:popEnterAnim="@anim/nav_default_pop_enter_anim"
            app:popExitAnim="@anim/nav_default_pop_exit_anim" />
    </fragment>
</navigation>

๐Ÿ‘‡๐Ÿผ As you can see below, First fragment has home icon. This means that we will see the First fragment on screen when app is opened. If we want to make Second fragment as home page, we can do it easily with right click on it and select "Set as Start Destination" button.

Screen Shot 2021-08-18 at 16 08 43

  • fragment_first.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"
   android:background="@color/gray"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   tools:context=".FirstFragment">

   <TextView
       android:id="@+id/tv_number"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="1"
       android:textColor="@color/white"
       android:textSize="100sp"
       android:textStyle="bold"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintHorizontal_bias="0.5"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • fragment_second.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"
    android:background="@color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:context=".SecondFragment">

    <TextView
        android:id="@+id/tv_number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:textColor="@color/white"
        android:textSize="100sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • FirstFragment.kt
class FirstFragment : Fragment() {

   override fun onCreateView(
       inflater: LayoutInflater, container: ViewGroup?,
       savedInstanceState: Bundle?
   ): View? {
       // Inflate the layout for this fragment
       val view = inflater.inflate(R.layout.fragment_first, container, false)

       view.setOnClickListener {
           Navigation.findNavController(view).navigate(R.id.navigateToSecondFragment)
       }

       return view
   }
}
  • SecondFragment.kt
class FirstFragment : Fragment() {

   override fun onCreateView(
       inflater: LayoutInflater, container: ViewGroup?,
       savedInstanceState: Bundle?
   ): View? {
       // Inflate the layout for this fragment
       val view = inflater.inflate(R.layout.fragment_second, container, false)

       view.setOnClickListener {
           Navigation.findNavController(view).navigate(R.id.navigateToFirstFragment)
       }

       return view
   }
}

navigation-component's People

Contributors

yagmurerdogan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.