Skip to content

Instantly share code, notes, and snippets.

View NezSpencer's full-sized avatar

Nnabueze Uhiara NezSpencer

  • Portugal
View GitHub Profile
@NezSpencer
NezSpencer / FragmentOne.kt
Created March 26, 2021 08:11
Using the safeNavigate option to prevent the Navigation action not found issue
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_open_screen_two.setOnClickListener {
findNavController().safeNavigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())
}
btn_double_click.setOnClickListener {
performDoubleClick()
}
@NezSpencer
NezSpencer / FragmentOne.kt
Created March 26, 2021 08:02
Using the safeNavigate extension method
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_open_screen_two.setOnClickListener {
findNavController().safeNavigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())
}
btn_double_click.setOnClickListener {
performDoubleClick()
}
@NezSpencer
NezSpencer / Util.kt
Created March 26, 2021 07:33
SafeNavigate extension methods
fun NavController.safeNavigate(direction: NavDirections) {
currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
}
fun NavController.safeNavigate(
@IdRes currentDestinationId: Int,
@IdRes id: Int,
args: Bundle? = null
) {
if (currentDestinationId == currentDestination?.id) {
@NezSpencer
NezSpencer / FragmentOne.kt
Created March 25, 2021 07:43
Using a blocking ClickListener to prevent multiple clicks within a specified waitTime
package com.nezspencer.navigationtest
import android.os.Bundle
import android.os.SystemClock
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.main.fragment_one.*
@NezSpencer
NezSpencer / FragmentOne.kt
Created March 20, 2021 09:58
Recreating the multiple click action that happens when a screen lags causing the "navigation action not found" error
package com.nezspencer.navigationtest
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.main.fragment_one.*
class FragmentOne : Fragment(R.layout.fragment_one) {
@NezSpencer
NezSpencer / DatePickerActivity.kt
Last active November 8, 2020 10:35
get the total time deviation(in milliseconds) of the current timezone from GMT
val calendar = Calendar.getInstance()
val utcOffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET)
@NezSpencer
NezSpencer / Util.kt
Created October 1, 2020 08:31
North american area codes. Could be used for phone number validation. North american phones must start with a valid area code
private val supportedNorthAmericanAreaCodes = listOf(
"201",
"202",
"203",
"204",
"205",
"206",
"207",
"208",
"209",
@NezSpencer
NezSpencer / styles.xml
Created March 4, 2020 10:58
DialogFragment style for custom width and height
<style name="KycDialogStyle" parent="android:Theme.Dialog">
<item name="android:windowBackground">@drawable/white_3dp_corner_radius</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">90%</item>
<item name="android:windowNoTitle">true</item>
</style>
@NezSpencer
NezSpencer / LoggerCrashHandler.kt
Created January 12, 2020 11:57
This crashHandler shows the crash logs on the device when the app crashes
import androidx.fragment.app.FragmentActivity
class LoggerCrashHandler(private val app: FragmentActivity) : Thread.UncaughtExceptionHandler {
private val defaultUEH: Thread.UncaughtExceptionHandler? =
Thread.getDefaultUncaughtExceptionHandler()
override fun uncaughtException(t: Thread, e: Throwable) {
var arr = e.stackTrace
var report = "$e\n\n"
report += "--------- Stack trace ---------\n\n"
@NezSpencer
NezSpencer / MainActivity.kt
Created August 11, 2019 12:45
using findViewById
val termsAndConditionsTextView = findViewById<TextView>(R.id.tv_terms_conditions)
termsAndConditionsTextView.text = termsCopy
termsAndConditionsTextView.movementMethod = LinkMovementMethod.getInstance()
termsAndConditionsTextView.highlightColor = Color.TRANSPARENT