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.
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
}
}
}
0 Comments