Skip to content

Instantly share code, notes, and snippets.

implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
@Target(AnnotationTarget.FUNCTION)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)
@Module
abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MainViewModel::class)
class ViewModelFactory<T : ViewModel> @Inject constructor(private val viewModelProvider: Provider<T>) :
ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return viewModelProvider.get() as T
}
}
// usage in the client code
@AlexMisiulia
AlexMisiulia / KeyboardShowing.kt
Created June 9, 2019 17:48
A code block which shows how to show Android keyboard after user press back button when current Activity has focus.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
startActivity(Intent(this, DetailsActivity::class.java))
}
@AlexMisiulia
AlexMisiulia / MainActivity.kt
Created September 16, 2019 05:42
EditText change listener
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
phoneNumberEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
dependencies {
implementation 'io.michaelrocks:libphonenumber-android:8.10.16'
}
class MainActivity : AppCompatActivity() {
private lateinit var phoneNumberUtil: PhoneNumberUtil
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
phoneNumberUtil = PhoneNumberUtil.createInstance(this)
}
}
private fun getCountryIsoCode(number: String): String? {
val validatedNumber = if (number.startsWith("+")) number else "+$number"
val phoneNumber = try {
phoneNumberUtil.parse(validatedNumber, null)
} catch (e: NumberParseException) {
Log.e(TAG, "error during parsing a number")
null
}
if(phoneNumber == null) return null
override fun afterTextChanged(editable: Editable?) {
val number = editable.toString()
val countryIsoCode = getCountryIsoCode(number)
countryTextView.text = countryIsoCode ?: "Can't detect a country"
}
package com.sharyfire.countryphonedetector
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import io.michaelrocks.libphonenumber.android.NumberParseException
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil
import io.michaelrocks.libphonenumber.android.Phonenumber.PhoneNumber