Skip to content

Instantly share code, notes, and snippets.

View wajahatkarim3's full-sized avatar
:octocat:
Subscribe to my newsletter: remotekaro.substack.com

Wajahat Karim wajahatkarim3

:octocat:
Subscribe to my newsletter: remotekaro.substack.com
View GitHub Profile
@wajahatkarim3
wajahatkarim3 / build.gradle
Created January 31, 2021 16:01
Project's root build.gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
kotlin_version = '1.4.21'
}
repositories {
google()
jcenter()
}
dependencies {
@wajahatkarim3
wajahatkarim3 / keys.xml
Created January 31, 2021 15:42
The keys.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_id">COMET_CHAT_APP_ID</string>
<string name="auth_key">COMET_CHAT_AUTH_KEY</string>
<string name="region">us</string>
</resources>
// 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{
// 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/*"
// 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
btnPick.setOnClickListener {
simpleContractRegistration(input = 3) // Remember, our SimpleContract had input of Int. So, 3 is dummy value here.
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// Other Methods
private val simpleContractRegistration = registerForActivityResult(SimpleContract()) {resultStr ->
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
{
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 22:00
The onActivityResult() method example
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) {