Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe

Wajahat Karim wajahatkarim3

:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe
View GitHub Profile
@wajahatkarim3
wajahatkarim3 / KIntent.kt
Created Feb 27, 2018 — forked from passsy/KIntent.kt
Kotlin extension functions to start a generic Activity
View KIntent.kt
package com.pascalwelsch.extensions
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
/**
* Extensions for simpler launching of Activities
@wajahatkarim3
wajahatkarim3 / ActivitiesLaunchingWay.kt
Last active Jun 10, 2020
Kotlin Extensions for simpler, easier and funw way of launching of Activities
View ActivitiesLaunchingWay.kt
/**
* Kotlin Extensions for simpler, easier and funw way
* of launching of Activities
*/
inline fun <reified T : Any> Activity.launchActivity (
requestCode: Int = -1,
options: Bundle? = null,
noinline init: Intent.() -> Unit = {})
{
View Permissions.kt
// Requesting Location Permission
bi.btnRequestPermission.setOnClickListener {
askLocationPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
}
// Single Permission Contract
private val askLocationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { result ->
if(result){
Log.e("TAG", "Location permnission granted")
}else{
View PickImages.kt
// Pick Images Contract - Normally this is for all kind of files.
private val pickImages = registerForActivityResult(ActivityResultContracts.GetContent()) {uri ->
uri?.let {uri ->
imageView.setImageURI(uri)
}
}
// Calling GetContent contract
pickImageButton.setOnClickListener {
pickImages("image/*") // We want images, so we set the mimeType as "image/*"
View TakePicture.kt
// TakePicture Contract Registration
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) {bitmap ->
bitmap?.let {
imageView.setImageBitmap(bitmap)
}
}
// Calling the takePicture contract
captureButton.setOnClickListener {
var imageUri: Uri? = null
View CallingContract.kt
btnPick.setOnClickListener {
simpleContractRegistration(input = 3) // Remember, our SimpleContract had input of Int. So, 3 is dummy value here.
}
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// Other Methods
private val simpleContractRegistration = registerForActivityResult(SimpleContract()) {resultStr ->
View SimpleContract.kt
class SimpleContract : ActivityResultContract<Integer, String?>() {
override fun createIntent(context: Context, input: Integer?): Intent {
var intent = Intent(context, MyChildActivity::class.java)
intent.putExtra("myInputKey", input)
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): String? = when
{
View Dependencies.gradle
implementation 'androidx.activity:activity-ktx:1.2.0-alpha04'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha04'
@wajahatkarim3
wajahatkarim3 / MainActivity.kt
Created May 1, 2020
The onActivityResult() method example
View MainActivity.kt
class MainActivity : AppCompatActivity()
{
// Other methods of your Activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Capture Image with Camera
if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null) {
You can’t perform that action at this time.