Created
September 16, 2019 05:45
-
-
Save AlexMisiulia/0cd8838b5eb6f5bcc2c395f6ef28fac4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
import kotlinx.android.synthetic.main.activity_main.* | |
private const val TAG = "MainActivity" | |
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) | |
phoneNumberEditText.addTextChangedListener(object : TextWatcher { | |
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
} | |
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
} | |
override fun afterTextChanged(editable: Editable?) { | |
val number = editable.toString() | |
val countryIsoCode = getCountryIsoCode(number) | |
countryTextView.text = countryIsoCode ?: "Can't detect a country" | |
} | |
}) | |
} | |
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 | |
return phoneNumberUtil.getRegionCodeForCountryCode(phoneNumber.countryCode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment