How to achieve password error like this in TextInputLayout Android? - android

This is what I want to achieve. And please help me with the regex for the third error.

You need to use isErrorEnabled and error atributes of your TextInputLayout instance.
const val PASSWORD_PATTERN = ...// your regex
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.checkPasswordButton.setOnClickListener {
val password = binding.passwordEditText.text.toString()
val isPasswordValid =
Pattern.compile(PASSWORD_PATTERN).matcher(password).matches()
binding.passwordInputLayout.isErrorEnabled = !isPasswordValid
val passwordError = if (isPasswordValid) "" else
getString(R.string.invalid_password)
binding.passwordInputLayout.error = passwordError
}
}
}

Related

I am confused with how lifecycleScope in android studio works

I am confused about how flow.collect works. Because in the lifecycleScope below I already say that a should be assigned by the value of data in my database. However, the value of a is still the string of "Hi" instead of "Hello".
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
private var a: String = "Hi"
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding?.root)
val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()
lifecycleScope.launch {
somethingDao.insert(SomethingModel("Hello"))
somethingDao.fetchAllSomething().collect {
a = it[it.size - 1].name
}
}
println(a)
}
}
this is all of the information in my database
lifecycleScope.launch will start a coroutine, to make it simple the code inside lifecycleScope.launch will be executed in another thread and it will take some time until inserting data and reading it from database, but println(a) is on the main thread so it will be executed before this line a = it[it.size - 1].name, so your println(a) should be inside lifecycleScope.launch like this:
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
private var a: String = "Hi"
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding?.root)
val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()
lifecycleScope.launch {
somethingDao.insert(SomethingModel("Hello"))
somethingDao.fetchAllSomething().collect {
a = it[it.size - 1].name
println(a)
}
}
}
}
Note: take a look on kotlin coroutines to better understand

How can I fix this? cant put setError to my editText

I make my first android app with Android Studio, and now I want to make a setError on my editText, but I cant, I only can put a toast
here is my code:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.calculateButton.setOnClickListener { calculateTip() }
}
private fun calculateTip() {
val stringInTextField = binding.costOfProduct.text.toString()
val stringInTextField1 = binding.amountOfProduct.text.toString()
if (stringInTextField.isEmpty()) {
Toast.makeText(this, "this cant be empty", Toast.LENGTH_SHORT).show()
return
}
val cost = stringInTextField.toDouble() / stringInTextField1.toDouble()
val tipPercentage = when (binding.productOptions.checkedRadioButtonId) {
R.id.option_normal -> 1.5
R.id.option_60 -> 1.6
R.id.option_70 -> 1.7
else -> 2.0
}
var product = tipPercentage * cost
val roundUp = binding.IVA.isChecked
if (roundUp) {
product = (tipPercentage * cost) * 1.21
}
val formattedTip = NumberFormat.getCurrencyInstance().format(product)
binding.result.text = getString(R.string.resultado, formattedTip)
}
}
You should use to setError()
Like this: stringInTextField.setError("this cant be empty")
For more information: Show Error on the tip of the Edit Text Android
you can use this following code according to your code:
if (stringInTextField.isEmpty()) { stringInTextField.setError("this cant be empty") return; }

Realm Kotlin - Delete realm object

https://www.mongodb.com/docs/realm/sdk/kotlin/realm-database/delete/delete-all-objects-of-a-type/
I am learning new kotlin-realm in my project. But i dont know how to delete objects. It keep showing error Caused by: io.realm.internal.interop.RealmCoreNotInATransactionException: [5]: Must be in a write transaction
this is the code :
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val realm by lazy { (application as CustomApplication).realm }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
realm.writeBlocking {
copyToRealm(Buku(
name = "Perjuangan Menjual Baju"
))
copyToRealm(Buku(
name = "Perang Saudara"
))
}
val buku = realm.query<Buku>("name BEGINSWITH $0", "pera")
Log.i("AOEU", "buku = $buku")
CoroutineScope(Dispatchers.Main).launch {
val query = realm.query<Buku>().find()
realm.write {
delete(query)
}
}
}
}
After days of finding solution. I just realized that the only mistake in my code is
val query = realm.query().find()
Which is should be replaced with
val query = this.query().find()

Can't access views or change their values

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
getCurrentCountry()
}
private fun getCurrentCountry(){
val api = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build().create(CountryReq::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.getCountries().awaitResponse()
if(response.isSuccessful){
val data = response.body()!!
withContext(Dispatchers.Main){
binding.selectCountry.text = "Demo text"
}
}
}
}
}
I'm trying to get data from an API and display it in a textview. But I can't seem to access the views or change its values from the GlobalScope.launch{} block.

Why didn't Moxy initialize the Presenter (Kotlin, Android)

I got error that mPresenter has not been initialized. I didn't understand WHY?
I got this error when I use Kotlin but if I use JAVA everything is fine
Here is my code
View
#StateStrategyType(value = AddToEndStrategy::class)
interface IHelloWorldView : MvpView {
fun showMessage(message: Int)
}
Presenter
#InjectViewState
class HelloWorldPresenter : MvpPresenter<IHelloWorldView>() {
fun show() = viewState.showMessage(R.string.message)
}
MainActivity
class MainActivity : MvpAppCompatActivity(), IHelloWorldView {
#InjectPresenter
lateinit var mPresenter: HelloWorldPresenter
lateinit var mText: TextView
lateinit var mButton: Button
override fun showMessage(message: Int) {
mText.setText(message)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mText = findViewById(R.id.text)
mButton = findViewById(R.id.button)
mButton.setOnClickListener {
mPresenter.show()
}
}
}
Because you need to init it like in JAVA Foo foo = new Foo();
It seems like you only created a variable
class MainActivity : MvpAppCompatActivity(), IHelloWorldView {
#InjectPresenter
lateinit var mPresenter: HelloWorldPresenter
//initialisation
#ProvidePresenterTag(presenterClass = HelloWorldPresenter::class, type =PresenterType.GLOBAL)
fun providemPresenter() = HelloWorldPresenter()
fun mPresenter() = HelloWorldPresenter()
lateinit var mText: TextView
lateinit var mButton: Button
override fun showMessage(message: Int) {
mText.setText(message)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mText = findViewById(R.id.text)
mButton = findViewById(R.id.button)
mButton.setOnClickListener {
mPresenter.show()
}
}
}
type = PresenterType.GLOBAL - do not exist (at least in kotlin)

Categories

Resources