This project demonstrates the usage of multiple fragments within an Android application using Kotlin and ViewBinding. The project includes a MainActivity
with three buttons to switch between different fragments (DefaultFragment
, BlankFragment1
, and BlankFragment2
).
The MainActivity
class initializes the default fragment (DefaultFragment
) and sets up button click listeners to switch between BlankFragment1
and BlankFragment2
. It uses ActivityMainBinding
for ViewBinding to inflate the layout and bind the views.
-
DefaultFragment:
- This is the initial fragment displayed when the application starts.
- It inflates a simple layout with minimal logic.
-
BlankFragment1:
- Uses
FragmentBlank1Binding
for ViewBinding. - Includes a sample Toast message to indicate when the fragment is loaded.
- Uses
-
BlankFragment2:
- A simple fragment that inflates its layout without additional logic.
-
activity_main.xml:
- Contains a
FrameLayout
with the IDframeLayout
to host the fragments. - Includes two buttons with IDs
button1
andbutton2
to switch between the fragments.
- Contains a
-
fragment_default.xml:
- Layout for
DefaultFragment
. - Contains a
TextView
displaying "Default Fragment".
- Layout for
-
fragment_blank1.xml:
- Layout for
BlankFragment1
. - Contains a
TextView
displaying "Blank Fragment 1".
- Layout for
-
fragment_blank2.xml:
- Layout for
BlankFragment2
. - Contains a
TextView
displaying "Blank Fragment 2".
- Layout for
-
Setup Android Studio:
- Open the project in Android Studio.
-
Run the App:
- Execute the app on an emulator or physical device.
-
Switch Fragments:
- Use the buttons in
MainActivity
to switch betweenDefaultFragment
,BlankFragment1
, andBlankFragment2
.
- Use the buttons in
- AndroidX: Ensure your project is using AndroidX libraries for compatibility.
- ViewBinding: Used for binding views in
MainActivity
andBlankFragment1
.
- The default fragment (
DefaultFragment
) is displayed when the app starts. - Clicking the buttons will replace the current fragment with
BlankFragment1
orBlankFragment2
. BlankFragment1
includes a Toast message to indicate its loading for demonstration purposes.