Skip to content

Instantly share code, notes, and snippets.

@ikhlaqmalik13
Created June 2, 2024 07:51
Show Gist options
  • Save ikhlaqmalik13/564b74554802b69d9e5b884435a728c5 to your computer and use it in GitHub Desktop.
Save ikhlaqmalik13/564b74554802b69d9e5b884435a728c5 to your computer and use it in GitHub Desktop.
Escports App building class In Kotlin - 2, Jun, 2024
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".esports.EsportsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/fl_fragment_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8" />
<FrameLayout
android:id="@+id/fl_fragment_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.maple.wordtrek.esports.utils
import com.maple.wordtrek.R
object AllControls {
fun getControls(): ArrayList<Control> {
val controls: ArrayList<Control> = ArrayList<Control>()
controls.add(Control(1, R.drawable.baseline_cottage_24, "Home"))
controls.add(Control(2, R.drawable.baseline_cottage_24, "Contact Us"))
controls.add(Control(3, R.drawable.baseline_cottage_24, "Tournaments"))
return controls
}
}
class Control(
val id: Int,
val iconId: Int,
val name: String
)
<?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/yellow"
tools:context=".esports.fragments.ContactUsFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
package com.maple.wordtrek.esports.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.maple.wordtrek.R
class ContactUsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_contact_us, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
ContactUsFragment().apply {
arguments = Bundle().apply {
}
}
}
}
package com.maple.wordtrek.esports.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.maple.wordtrek.databinding.FragmentControllerBinding
import com.maple.wordtrek.databinding.RowSingleControlViewBinding
import com.maple.wordtrek.esports.utils.AllControls
class ControllerFragment : Fragment() {
private lateinit var binding: FragmentControllerBinding
private lateinit var onRowClick: (id: Int) -> Unit
private fun setCallbacks(onRowClick: (id: Int) -> Unit) {
this.onRowClick = onRowClick
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
) = FragmentControllerBinding.inflate(layoutInflater).also {
binding = it
}.root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpControls()
}
private fun setUpControls() {
AllControls.getControls().forEach { control ->
val rowSingleControlViewBinding = RowSingleControlViewBinding.inflate(layoutInflater)
rowSingleControlViewBinding.ivControl.setImageResource(control.iconId)
rowSingleControlViewBinding.tvControlName.text = control.name
rowSingleControlViewBinding.root.setOnClickListener {
onRowClick(control.id)
}
binding.llControls.addView(rowSingleControlViewBinding.root)
}
}
companion object {
val TAG2 = ControllerFragment::class.java.canonicalName
@JvmStatic
fun newInstance(onRowClick: (id: Int) -> Unit) =
ControllerFragment().apply {
arguments = Bundle().apply {
}
setCallbacks(onRowClick)
}
}
}
package com.maple.wordtrek.esports
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.maple.wordtrek.databinding.ActivityEsportsBinding
import com.maple.wordtrek.esports.fragments.ContactUsFragment
import com.maple.wordtrek.esports.fragments.ControllerFragment
import com.maple.wordtrek.esports.fragments.ViewerFragment
import com.maple.wordtrek.home.fragments.HomeFragment
class EsportsActivity : AppCompatActivity() {
private var onRowClick: ((Int) -> Unit)? = null
private lateinit var binding: ActivityEsportsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityEsportsBinding.inflate(layoutInflater)
setContentView(binding.root)
setUpControlsFragment()
}
private fun setUpControlsFragment() {
var controlsFragment = supportFragmentManager.findFragmentByTag(ControllerFragment.TAG2)
if(controlsFragment == null){
controlsFragment = ControllerFragment.newInstance(onRowClick = {
setUpViewFragment(it)
})
}
supportFragmentManager.beginTransaction().apply {
replace(
binding.flFragmentController.id,
controlsFragment,
ControllerFragment.TAG2
)
commit()
}
}
private fun setUpViewFragment(id: Int) {
when(id){
1 -> {
setUpViewFragmentOfId(HomeFragment.newInstance())
}
2 -> {
setUpViewFragmentOfId(ContactUsFragment.newInstance())
}
3 -> {
setUpViewFragmentOfId(ViewerFragment.newInstance())
}
else -> {
}
}
}
private fun setUpViewFragmentOfId(fragment: Fragment) {
var viewFragment = supportFragmentManager.findFragmentByTag(fragment::class.java.canonicalName)
if(viewFragment == null){
viewFragment = fragment
}
supportFragmentManager.beginTransaction().apply {
replace(
binding.flFragmentView.id,
viewFragment,
fragment::class.java.canonicalName
)
commit()
}
}
}
<?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/white"
tools:context=".esports.fragments.ControllerFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:id="@+id/ll_controls"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</ScrollView>
</FrameLayout>
<?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/color_light_gray"
tools:context=".esports.fragments.HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hoME FRAGMENT" />
</FrameLayout>
package com.maple.wordtrek.esports.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.maple.wordtrek.R
class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home2, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
HomeFragment().apply {
arguments = Bundle().apply {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="12dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_control"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/baseline_cottage_24"/>
<TextView
android:id="@+id/tv_control_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Home"
android:textAlignment="center"
android:textColor="@color/black"
android:textStyle="bold" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment