Mehedi Hassan Piash | Senior Software Engineer | Android | iOS | KMP | Ktor | Jetpack Compose | React-Native.

July 28, 2021

Image picker and convert uri to file

July 28, 2021 Posted by Piash , No comments
 1. Fetch uri with https://github.com/Dhaval2404/ImagePicker ImagePicker.with(this) .compress(1024) //Final image size will be less than 1 MB(Optional) .maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional) .createIntent { intent -> startForProfileImageResult.launch(intent) } private val startForProfileImageResult...

July 19, 2021

onActivity result for fragment navigation component

July 19, 2021 Posted by Piash , No comments
 1. Get result after navigationUp in the previous fragment  setFragmentResultListener("requestKey") { requestKey, bundle -> // We use a String here, but any type that can be put in a Bundle is supported val result = bundle.getParcelable<Address>("bundleKey") Toast.makeText(context, "$result", Toast.LENGTH_SHORT).show() }  1. Set data for...

OnActivityResult method is deprecated, what is the alternative?

July 19, 2021 Posted by Piash , No comments
 1. Call another activity with contract api resultContract.launch(requireContext().openActivityResult<DetailActivity>()) 2. Call an activity for the result  private val resultContract = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (it.resultCode == Activity.RESULT_OK) { // get the result here } ...

July 11, 2021

Json extension for kotlin

July 11, 2021 Posted by Piash , No comments
// Transform simple object to String with Gson fun <reified T : Any> T.toPrettyJson() : String = Gson().toJson(this, T::class.java) // Transform String Json to Object inline fun <reified T : Any> String.fromPrettyJson() : T = Gson().fromJson(this , T::class.java) // Transform String List Json to Object inline fun <reified T : Any> String.fromPrettyJsonList()...

July 08, 2021

Click again to exit the app in navigation component [kotlin]

July 08, 2021 Posted by Piash , No comments
class MainActivity : BaseActivity() { private lateinit var navController: NavController private lateinit var navHostFragment: NavHostFragment private var doubleBackToExitPressedOnce = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) ...

July 07, 2021

Prevent or avoid Multiple clicks in Android app [Kotlin]

July 07, 2021 Posted by Piash , No comments
 Step 1 : Create class with name SafeClickListener.kt class SafeClickListener( private var defaultInterval: Int = 1000, private val onSafeCLick: (View) -> Unit ) : View.OnClickListener { private var lastTimeClicked: Long = 0 override fun onClick(v: View) { if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultInterval) { return } lastTimeClicked...