Declare a fragment

- Posted in Uncategorized by

To simply declare a fragment that you can call later in your code:

class SecondFragSampleName : Fragment(R.layout.second_fragment)


or if you want to specify extra methods that will run when the fragment becomes active.

class SecondFragSampleName : Fragment(R.layout.second_fragment) {
  // Declare methods here, ie, OnCreate, etc.
}


Notes:

  • Do not declare this inside the main activity fragment. Suggested placement is after the main activity fragment.
  • Ensure that a second_fragment layout exists, and that it contains the androidx.fragment.app.FragmentContainerView element.


Then when you want to switch to this "second fragment", add the following code as an action/method on the currently active fragment:

supportFragmentManager
    .beginTransaction()
    .setTransition(TRANSIT_FRAGMENT_OPEN)
    .setReorderingAllowed(true)
    .addToBackStack("DefaultView")
    .add(R.id.map, SecondFragSampleName())
    .commit()

Create a snackbar

- Posted in Uncategorized by

Kotlin code to create and configure a snackbar:

1 
2 
3 
4 
5 
6 
7 
8 
9 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    // Run this action if user clicks on snackbar Action Text
    class CustomSnackBarAction : View.OnClickListener {
        override fun onClick(v: View) {
            oToast("This device is not supported.")
           }
        }

    // Declare snackbar variables
    val currentView: View = this.findViewById(android.R.id.content)
    val snackBar = Snackbar.make(
        currentView,
        "This text will appear on the snackbar.",
        Snackbar.LENGTH_INDEFINITE
        )

    // Specify how Action Text should look and behave
    snackBar.setAction("OK", CustomSnackBarAction())
    snackBar.setActionTextColor(Color.WHITE)
    snackBar.setTextColor(Color.YELLOW)
    snackBar.setMaxInlineActionWidth(500)

    // Configure background of the snackbar
    val snackBarView = snackBar.view
    snackBarView.setBackgroundColor(Color.DKGRAY)

    // Configure text on the snackbar
    val textView =
        snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
    textView.setTextColor(Color.YELLOW)
    textView.textSize = 28f
    textView.maxLines = 5

    // Show the snackbar
    snackBar.show()


Where:

  • 2️⃣ - Declare a function that runs whenever the user clicks the Action Text on the snackbar
  • 4️⃣ - This is a custom function to generate a Toast message.
  • 9️⃣ - Find current view and assign it to a variable
  • 🔟 - Create a snackbar, but don't show it yet
  • 1️⃣3️⃣ - Specify Snackbar.LENGTH_INDEFINITE so snackbar does not go away until user clicks the Action Text
  • 1️⃣7️⃣ - Set the custom action that occurs if users click the Action Text
  • 2️⃣4️⃣ - Set a custom bgcolor for the snackbar
  • 2️⃣7️⃣ - Find snackbar text so we can modify its style
  • 3️⃣1️⃣ - Number of maximum lines that the snackbar supports. If not set, Android Studio uses the default value of 1and will only show the text on one line.
  • 3️⃣4️⃣ - Show the snackbar to the world

Private function for toast

- Posted in Uncategorized by

Created a private function so I don't need to write very long and verbose syntax just to show a Toast. private fun oToast(text: String) {...

Check if Google Play Services is available

- Posted in Uncategorized by

Check if Google Play Services is available.

private fun checkPlayServices(): Boolean {
    val apiAvailability = GoogleApiAvailability.getInstance()
    val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)

    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
              // Do stuff if Google Play is not available 
        } else {
              // Do stuff if Google Play is available 
            finish()
        }
        return false
    }
    return true
}