10 Useful Kotlin Extension Functions for Android Developers

<p>Thanks for showing your love for the Kotlin extension functions in the first part. I would like to share more extensions to help you improve your programming experience with Android and Kotlin. If you have not read it yet, you can check it out at the link below.</p> <h2>10 Useful Kotlin Extension Functions for Android Developer</h2> <h3>Improve android development experience using extension function</h3> <p>medium.com</p> <h2>Prerequisite</h2> <p>Should have basic knowledge of kotlin syntax and android</p> <blockquote> <p>Start working with new chapter&hellip;</p> </blockquote> <h2>Value from EditText</h2> <p>Through the text property of&nbsp;<code>EditText</code>, you can quickly obtain the text from EditText. But, it&rsquo;s&nbsp;<code>Editable</code>. So, we have to convert it to a string for each and every time to get the exact value from EditText. But, here is the catch, you can easily get value from EditText using the extension property listed below.</p> <p><strong>Usage</strong></p> <pre> val name = etName.value</pre> <h2>Start Activity</h2> <p>Start Activity is a common way to transition to another activity. You must first address the intent of the target activity. However, by using this extension function, you can eliminate the intent creation part.</p> <p>You can also able to customize the intent to pass some data and other things. It&rsquo;s also able to kill the current calling activity if you want. Look at the examples below.</p> <p><strong>Usage</strong></p> <pre> startActivity(MainActivity::class.java) // Without Intent modification startActivity(MainActivity::class.java) { // You can access the intent object in this block putExtra(&quot;key&quot;, &quot;value&quot;) }</pre> <h2>Check Network</h2> <p>Nowadays, all our applications have internet connectivity as per our requirements, features, and functionality. So, you may have to check whether an internet connection is available or not.</p> <p>So, this extension function is useful for checking the internet connection in activity and fragment. It is simple to utilize in the if statement and other locations inside the scope.</p> <p><strong>Usage</strong></p> <pre> if (isNetworkAvailable()) { // Called when network is available } else { // Called when network not available }</pre> <h2>Check Permission</h2> <p>To fulfill our use cases, you might occasionally have to play with any permission. So, we may have to verify whether the permission is granted or not for our application. Therefore, the extension function below is useful for determining whether or not permission has been given.</p> <p><strong>Usage</strong></p> <pre> if (isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION)) { // Block runs if permission is granted } else { // Ask for permission }</pre> <h2>Remove whitespaces</h2> <p>Sometimes we may have unrealistic text data from Rest API or some other data source. So, if you have more than one whitespace and you want to remove it then we can use&nbsp;<code>removeDuplicateWhitespaces()</code>&nbsp;or if you want to completely remove it then you can use&nbsp;<code>removeAllWhitespaces()</code>extension functions.</p> <p><a href="https://medium.com/@akshay.kalola28/10-useful-kotlin-extension-functions-for-android-developers-2-c063d0b1464e">Visit Now</a></p> <p>&nbsp;</p>