Kotlin Validation for Edit-text

 Most mobile apps are designed to intelligently handle Users information. This helps to prevent some potential inconveniences which may sometimes result into even worse scenarios such as crashes. I would not speak for any other developers out there, but one thing i hate most, are crashes .Take an example you have an edit text with input type of number , the number you take in goes to perform some arithmetic operations say. A user may unknowingly put in a string which is then taken as a subject for calculations. This results into a fatal error, definitely.

Forcing User to Input Something in EditText in Android - Stack Overflow

We need a way to handle this information before we can actually process it. A situation may happen when empty spaces are taken for granted and they would cause serious trouble with passwords. In this illustration we are going to deal with checking an input field for any emptiness before submission, plus trimming empty spaces. 

A sample of some code i will be using in my layout XML file are right below, you can skim along and get some insights, if any....


<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="100dp"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:hint="@string/bidhaaLongDesc"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp">

<EditText
android:id="@+id/editMaelezoFull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autofillHints="name"
android:inputType="text"
android:textSize="16sp"/>

</com.google.android.material.textfield.TextInputLayout>

Well, i was using a linear layout as the parent, so you might not see the constraints in case you are used to the layout. You can use any layout you want, it's gonna produce the same outcome, coz the layouts do not matter a lot in this case.

Moving to the corresponding Kotlin files 

So, let's be on the same page here. To validate if we are good to go or something is wrong with the inputs, i am going to create a method that returns a boolean. A true to indicate everything is cool, and a false for the opposite.

 private fun mkaguzi(): Boolean{
return false
}

 A sample of our method is right there. Currently it returns a false value, which we have agreed is going to symbolize something feels not right. The mission is to make the function return true when all is cool and otherwise if something feels sneaky. We need a conditional statement for this kind of filter task. I'll be using a When statement, so try and roll with me and later you could try some other kind of conditional statement just to make sure you are all muscled up with my article.

private fun mkaguzi(): Boolean{
return when{

TextUtils.isEmpty(gonding.editMaelezoFull.text.toString().trim{ it <= ' '}) ->{
false
}


else ->{
true
}
}
}

 Let's go through what's happening right now. We are all aware what we are processing here is all in form of text, right? So TextUtils pops right in. A simple description of what happening up there is that i am using view binding, so gonding is my binding feature from my layout. If you still use findViewById you don't wanna have that.
editFullMaelezo is the actual edit text of mine which i then convert the text in there into an actual string then i trim empty spaces using the handy utility trim { it <= ' '}. 
Something very interesting is the fact that, the whole line of TextUtils is a boolean value, which is either true if the edit text is empty and false if it is not. So if it surely is, we gonna return false for our method or else true which will provide a go ahead, for all is Clean.
When exactly do we call the method? Well you at least need to have a button or something where you will set an OnClickListener so when it's triggered, the method is called and a sign is returned.

That's all you get from me for this case. Thank you for your time, till next time
#Sam


0 Comments