LibGDX: Andrid Fragment
LibGDX: Android Fragment, ViewBinding, ConstraintLayout, NavigationComponents.
3 min readOct 28, 2021
This article describes the process of creating an LibGDX fragment-based project using: ViewBinding, ConstraintLayout, NavigationComponents.
GIT: https://github.com/Vladislav-Shesternin/LibGDX-with-AndroidFragment
Source: https://github.com/libgdx/libgdx/wiki/Starter-classes-and-configuration#fragment-based-libgdx
The article describes the basic minimum setup, for advanced setup download and test with a git project.
1. Create a standard LibGDX project.
2. Setting up the gradle:
build.gradle(Project)
def fragment_version = "1.3.6"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
def constraintLayout_version = "2.1.1"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayout_version"
def navigation_version = '2.3.5'
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
build.gradle(module: android)
gradle.properties
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
3. Create LibGdxFragment:
class LibGDXFragment: AndroidFragmentApplication() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val conf = AndroidApplicationConfiguration().apply {
useAccelerometer = false
useCompass = false
}
return initializeForView(FragmentTestGame(), conf)
}
}
FragmentTestGame — this is ApplicationListener from core module:
4. Create MenuFragment:
class MenuFragment : Fragment() {
private lateinit var binding: FragmentMenuBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentMenuBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.root.setOnClickListener {
navController.navigate(R.id.libGDXFragment)
}
}
}
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7D0F9C">
<TextView
android:id="@+id/tv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu Fragment\nclick"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
5. Create nav_graph:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="@id/menuFragment">
<fragment
android:id="@+id/menuFragment"
android:name="com.veldan.fragment_test.fragments.MenuFragment"
android:label="MenuFragment"
tools:layout="@layout/fragment_menu" />
<fragment
android:id="@+id/libGDXFragment"
android:name="com.veldan.fragment_test.fragments.LibGDXFragment"
android:label="LibGDXFragment" />
</navigation>
6. Create 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#830ECC">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
7. Final note — setUp AndroidLauncher:
package com.veldan.fragment_test
import android.app.Activity
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import com.veldan.fragment_test.databinding.ActivityMainBinding
lateinit var activity: Activity private set
lateinit var navController: NavController private set
lateinit var activityMainBinding: ActivityMainBinding private set
class AndroidLauncher : FragmentActivity(), AndroidFragmentApplication.Callbacks {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
activity = this
navController = findNavController(R.id.nav_host_fragment)
}
override fun exit() {}
}
PS. Vel_daN: Love what You DO 💚.