Skip to content

Instantly share code, notes, and snippets.

@Seniru
Created June 12, 2025 10:46
Show Gist options
  • Save Seniru/4eb621ecb68868377fc85b719978be0e to your computer and use it in GitHub Desktop.
Save Seniru/4eb621ecb68868377fc85b719978be0e to your computer and use it in GitHub Desktop.
Kotlin fragment example. This gist only showcases the logic required for fragment switching. You may you have to implement the layouts yourself if you wish to run this.
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.seniru.fragmentexample.R
// this method doesn't initialize the instance with the fragment layout.
// one must override the onCreateView to inflate the fragment
class Fragment1 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val rootView = inflater.inflate(R.layout.fragment_1, container,false)
// fragment specific stuff
val fragmentButton: Button = rootView.findViewById(R.id.fragment1Button)
fragmentButton.setOnClickListener {
Toast.makeText(context, "Hello from fragment 1", Toast.LENGTH_SHORT).show()
}
return rootView
}
}
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.seniru.fragmentexample.R
// inflate the layout in the constructor. This is another method
// when using this method override the onViewCreated method instead of the onCreateView method
class Fragment2 : Fragment(R.layout.fragment_2) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val button: Button = view.findViewById(R.id.frag2Button)
// fragment specific stuff
button.setOnClickListener {
Toast.makeText(context, "Hello from fragment 2", Toast.LENGTH_SHORT).show()
}
}
}
package com.seniru.fragmentexample
import Fragment1
import Fragment2
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val frag1Button: Button = findViewById(R.id.frag1Button)
val frag2Button: Button = findViewById(R.id.frag2Button)
// replace the container with fragment 1 when fragment 1 button is clicked
frag1Button.setOnClickListener {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, Fragment1())
.commit()
}
// replace the container with fragment 2 when fragment 2 button is clicked
frag2Button.setOnClickListener {
supportFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, Fragment2())
.commit()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment