Android Fragment Example Kotlin

06-Dec-2024

Android Fragment Example Kotlin




Android Fragment Example Kotlin


1/  Create A New Project .




<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/white"
android:orientation="vertical"
>

<FrameLayout
android:id="@+id/fmcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav_menu"
android:background="#199E92"
/>

</RelativeLayout>



2/  Create a menu directory .

menu<new<Menu Resource File<bottom_nav_menu.xml




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

<item
android:title="Home"
android:id="@+id/home"
android:icon="@drawable/home"
/>

<item
android:title="Notification"
android:id="@+id/notification"
android:icon="@drawable/notification"
/>

<item
android:title="Settings"
android:id="@+id/settings"
android:icon="@drawable/settings"
/>

</menu>


3/MainActivity.kt


package com.dailydeen.practiceproject

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

lateinit var bottom_navigation: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottom_navigation=findViewById(R.id.bottom_navigation)

LoadFragment(Home()) ///By default open fragment

bottom_navigation.setOnItemSelectedListener{menuItem->

val fragment: Fragment = when (menuItem.itemId){
R.id.home -> Home() ///// create Home Fragment
R.id.notification -> Notification() ///// create Notification Fragment
R.id.settings -> Settings() ///// create Settings Fragment

else -> Home()
}

LoadFragment(fragment)
true

}

}

private fun LoadFragment(fragment: Fragment){
supportFragmentManager.beginTransaction()
.replace(R.id.fmcontainer,fragment)
.commit()
}

}



4/  Create A Fragment

>>Home  Fragment


<?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"
tools:context=".Home" android:gravity="center"
android:background="@color/white"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Home Fragment"
android:textSize="30sp"
android:gravity="center"
android:textColor="#009688"
android:textStyle="bold"
/>

</LinearLayout>

5/  

>>>Home.kt


package com.dailydeen.practiceproject

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class Home : Fragment() {


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


//////////// All functional work doing here-------------->

return myview
}


}

6/  Create A Fragment

>>Notification Fragment




<?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"
tools:context=".Notification"
android:gravity="center"
android:background="@color/white"

>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notification Fragment"
android:textSize="30sp"
android:gravity="center"
android:textColor="#009688"
android:textStyle="bold"
/>

</LinearLayout>

7/

>>> Notification.kt




package com.dailydeen.practiceproject

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


class Notification : Fragment() {

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


//////////// All functional work doing here-------------->

return myview
}

}

8/  Create A Fragment

>>Settings Fragment




<?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"
tools:context=".Settings"
android:gravity="center"
android:background="@color/white"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings Fragment"
android:textSize="30sp"
android:gravity="center"
android:textColor="#009688"
android:textStyle="bold"
/>

</LinearLayout>

9/ 

>>> Settings.kt


package com.dailydeen.practiceproject

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


class Settings : Fragment() {


@SuppressLint("MissingInflatedId")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val myview:View=inflater.inflate(R.layout.fragment_settings, container, false)


//////////// All functional work doing here-------------->

return myview
}


}

Comments