Skip to content

πŸ§œπŸΌβ€β™€οΈ Simple project examples to understand Navigation Component

Notifications You must be signed in to change notification settings

yagmurerdogan/Navigation-Component

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

57 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ‘©πŸ»β€πŸ’» Navigation Component Projects

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

<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>

πŸ‘‡πŸΌ 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:https://schemas.android.com/apk/res/android"
   xmlns:app="http:https://schemas.android.com/apk/res-auto"
   xmlns:tools="http:https://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:https://schemas.android.com/apk/res/android"
    xmlns:app="http:https://schemas.android.com/apk/res-auto"
    xmlns:tools="http:https://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
   }
}

Releases

No releases published

Packages

No packages published

Languages