Instantly share code, notes, and snippets.
Created
December 7, 2023 11:20
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save amoozeshbebin/ceda1b8d3385c76cef52aa8c55bc4bef to your computer and use it in GitHub Desktop.
Navigation Fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
activity_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" | |
tools:context=".MainActivity"> | |
<androidx.fragment.app.FragmentContainerView | |
android:id="@+id/fragmentContainerView" | |
android:name="androidx.navigation.fragment.NavHostFragment" | |
android:layout_width="409dp" | |
android:layout_height="729dp" | |
app:defaultNavHost="true" | |
app:navGraph="@navigation/main_navigation" | |
tools:layout_editor_absoluteX="1dp" | |
tools:layout_editor_absoluteY="1dp" | |
tools:ignore="MissingConstraints" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
Blank Fragment fragment_start.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
android:background="@color/purple" | |
tools:context=".StartFragment"> | |
<!-- TODO: Update blank fragment layout --> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:textSize="60sp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:textColor="@color/white" | |
android:text="Start Game" /> | |
<Button | |
android:id="@+id/startBtn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Start Game" | |
android:layout_gravity="center|bottom" | |
android:layout_marginBottom="20dp" | |
android:outlineAmbientShadowColor="@color/white" | |
android:outlineSpotShadowColor="@color/white" | |
android:textColorHighlight="@color/white"/> | |
</FrameLayout> | |
Blank Fragment fragment_running.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
android:background="@color/teal" | |
tools:context=".RunningFragment"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:textSize="60sp" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:text="Rinning" /> | |
<Button | |
android:id="@+id/runningbtn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Finish Game" | |
android:layout_gravity="center|bottom" | |
android:layout_marginBottom="20dp" | |
android:outlineAmbientShadowColor="@color/white" | |
android:outlineSpotShadowColor="@color/white" | |
android:textColorHighlight="@color/white"/> | |
</FrameLayout> | |
Blank fragment fragment_game_over.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
android:background="@color/purple2" | |
tools:context=".GameOverFragment"> | |
<!-- TODO: Update blank fragment layout --> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:textSize="60sp" | |
android:textColor="@color/white" | |
android:layout_gravity="center" | |
android:gravity="center" | |
android:text="Game is Over" /> | |
<Button | |
android:id="@+id/gameoverbtn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="End Game" | |
android:layout_gravity="center|bottom" | |
android:layout_marginBottom="20dp" | |
android:outlineAmbientShadowColor="@color/white" | |
android:outlineSpotShadowColor="@color/white" | |
android:textColorHighlight="@color/white"/> | |
</FrameLayout> | |
create a android resurses file in res as navigation and the name of navigation and main_navigation.xml and connect all fragment | |
Start_fragment.kt: | |
package com.iliyadev.navigationfragment | |
import android.os.Bundle | |
import androidx.fragment.app.Fragment | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Button | |
import androidx.navigation.Navigation | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private const val ARG_PARAM1 = "param1" | |
private const val ARG_PARAM2 = "param2" | |
/** | |
* A simple [Fragment] subclass. | |
* Use the [StartFragment.newInstance] factory method to | |
* create an instance of this fragment. | |
*/ | |
class StartFragment : Fragment() { | |
// TODO: Rename and change types of parameters | |
private var param1: String? = null | |
private var param2: String? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
arguments?.let { | |
param1 = it.getString(ARG_PARAM1) | |
param2 = it.getString(ARG_PARAM2) | |
} | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_start, container, false) | |
} | |
companion object { | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param param1 Parameter 1. | |
* @param param2 Parameter 2. | |
* @return A new instance of fragment StartFragment. | |
*/ | |
// TODO: Rename and change types and number of parameters | |
@JvmStatic | |
fun newInstance(param1: String, param2: String) = | |
StartFragment().apply { | |
arguments = Bundle().apply { | |
putString(ARG_PARAM1, param1) | |
putString(ARG_PARAM2, param2) | |
} | |
} | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val btn = view.findViewById<Button>(R.id.startBtn) | |
val navControl = Navigation.findNavController(view) | |
btn.setOnClickListener { | |
navControl.navigate(R.id.action_startFragment_to_runningFragment) | |
} | |
} | |
} | |
RunningFragment.kt: | |
package com.iliyadev.navigationfragment | |
import android.os.Bundle | |
import androidx.fragment.app.Fragment | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Button | |
import androidx.navigation.Navigation | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private const val ARG_PARAM1 = "param1" | |
private const val ARG_PARAM2 = "param2" | |
/** | |
* A simple [Fragment] subclass. | |
* Use the [RunningFragment.newInstance] factory method to | |
* create an instance of this fragment. | |
*/ | |
class RunningFragment : Fragment() { | |
// TODO: Rename and change types of parameters | |
private var param1: String? = null | |
private var param2: String? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
arguments?.let { | |
param1 = it.getString(ARG_PARAM1) | |
param2 = it.getString(ARG_PARAM2) | |
} | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_running, container, false) | |
} | |
companion object { | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param param1 Parameter 1. | |
* @param param2 Parameter 2. | |
* @return A new instance of fragment RunningFragment. | |
*/ | |
// TODO: Rename and change types and number of parameters | |
@JvmStatic | |
fun newInstance(param1: String, param2: String) = | |
RunningFragment().apply { | |
arguments = Bundle().apply { | |
putString(ARG_PARAM1, param1) | |
putString(ARG_PARAM2, param2) | |
} | |
} | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val btn = view.findViewById<Button>(R.id.runningbtn) | |
val navControl = Navigation.findNavController(view) | |
btn.setOnClickListener { | |
navControl.navigate(R.id.action_runningFragment_to_gameOverFragment) | |
} | |
} | |
} | |
GameOverFragment.kt: | |
package com.iliyadev.navigationfragment | |
import android.os.Bundle | |
import androidx.fragment.app.Fragment | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Button | |
import androidx.navigation.Navigation | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private const val ARG_PARAM1 = "param1" | |
private const val ARG_PARAM2 = "param2" | |
/** | |
* A simple [Fragment] subclass. | |
* Use the [GameOverFragment.newInstance] factory method to | |
* create an instance of this fragment. | |
*/ | |
class GameOverFragment : Fragment() { | |
// TODO: Rename and change types of parameters | |
private var param1: String? = null | |
private var param2: String? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
arguments?.let { | |
param1 = it.getString(ARG_PARAM1) | |
param2 = it.getString(ARG_PARAM2) | |
} | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_game_over, container, false) | |
} | |
companion object { | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param param1 Parameter 1. | |
* @param param2 Parameter 2. | |
* @return A new instance of fragment GameOverFragment. | |
*/ | |
// TODO: Rename and change types and number of parameters | |
@JvmStatic | |
fun newInstance(param1: String, param2: String) = | |
GameOverFragment().apply { | |
arguments = Bundle().apply { | |
putString(ARG_PARAM1, param1) | |
putString(ARG_PARAM2, param2) | |
} | |
} | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val btn = view.findViewById<Button>(R.id.gameoverbtn) | |
val navControl = Navigation.findNavController(view) | |
btn.setOnClickListener { | |
navControl.navigate(R.id.action_gameOverFragment_to_startFragment) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment