so I have this problem where I am trying to make Random Number Generator app on android. Basically you set the minimum and the maximum number and then it randomly picks numbers between min and max.
However my problem comes if the min or max TextEdit field is empty, the app crashes. I would like to display "X" on the screen. How to check if the field is empty or not?
I am using kotlin and here is sample of my code. I am begginer so please do not flame me if the code is wrong :)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
//val seekBar = findViewById<SeekBar>(R.id.seekBar)
val min = findViewById<TextView>(R.id.number_min)
val max = findViewById<TextView>(R.id.number_max)
rollButton.setOnClickListener {
if(min.text.toString().toInt()>= 0 && max.text.toString().toInt() <= 1000){
val rand = Random.nextInt(min.text.toString().toInt(),max.text.toString().toInt()+1)
resultsTextView.text = rand.toString()
}
else if(min.text.toString().isNullOrBlank()){
resultsTextView.text = "X"
}
else{
resultsTextView.text = "X"
}
}
}
}
To check if your EditText is empty use isNullOrEmpty(). This will check if your field is empty or is null, like the method name says. Here is an example:
val editTextString :String = editText.text.toString()
if(!editTextString.isNullOrEmpty()) //returns true if string is null or empty
There is another approach with TextUtils but since you are using Kotlin this approach is better.
EDIT:
You are doing this:
if(min.text.toString().toInt()>= 0 && max.text.toString().toInt() <= 1000){
val rand = Random.nextInt(min.text.toString().toInt(),max.text.toString().toInt()+1)
resultsTextView.text = rand.toString()
}
and here this line min.text.toString().toInt() is throwing you an exception. The reason for this is because currently min or max are empty String. So compailer can't format number from an String equals to "". You should do it like this:
if(!min.text.toString().isNullOrEmpty() && !max.text.toString().isNullOrEmpty() && min.text.toString().toInt()>= 0 && max.text.toString().toInt() <= 1000){
val rand = Random.nextInt(min.text.toString().toInt(),max.text.toString().toInt()+1)
resultsTextView.text = rand.toString()
}
I hope this works. If not, then take this into two IF statements like this:
if(min.text.toString().isNullOrEmpty() || max.text.toString().isNullOrEmpty() {
resultsTextView.text = "X"
} else if(min.text.toString().toInt() >= 0 && max.text.toString().toInt() <= 1000) {
val rand = Random.nextInt(min.text.toString().toInt(), max.text.toString.toInt()+1)
resultsTextView.text = rand.toString()
}
The second approach is maybe an even better and cleaner version since you don't have to check for anything else later.
Related
I'm working on creating a way to input an amount and format it from left to right with placeholder zeros.
For example, pressing 1 and 2 would show $0.12 pressing 3 would give $1.23. Pressing backspace would give $0.12.
Instead, I am getting $1,0002.00
binding.keypad.btnBackspace.setOnClickListener {
val text = binding.tvTotalValue.text.toString()
if(text.isNotEmpty()) {
binding.tvTotalValue.text = text.drop(1)
}
binding.tvTotalValue.text = ""
}
binding.keypad.onNumberSelected = {
processNewAmount(it.toString())
}
private fun processNewAmount(newValue: String) {
val text = binding.tvTotalValue.text.toString().plus(newValue)
val result = text.replace("[^0-9]".toRegex(), "") // remove any characters
val amount = result.toDouble()
binding.tvTotalValue.text = NumberFormat.getCurrencyInstance().format(amount)
}
What am I doing wrong?
Is there a better way to do this?
I advise keeping a property that stores the entered value without formatting. Each time a number is added, you can add it to this entered number and then format it for the screen. That will be a lot simpler than trying to move/remove existing symbols around in the String.
private var enteredNumber = ""
//...
binding.keypad.btnBackspace.setOnClickListener {
enteredNumber = enteredNumber.dropLast(1)
refreshTotal()
}
binding.keypad.onNumberSelected = {
if(!(it == 0 && enteredNumber == "0")) { // avoid multiple zeros or backspace will act unexpectedly
enteredNumber += it.toString()
refreshTotal()
}
}
//...
private fun refreshTotal() {
val amount = enteredNumber.toDouble() / 100.0
binding.tvTotalValue.text =
NumberFormat.getCurrencyInstance().format(amount)
}
i'm referring this answer https://stackoverflow.com/a/42802060/12428090
basically my goal is to handle word count of 100 words
the abve solution works fine for typed 100 words and even handles copy paste very well
but it doesnt handles the new line case
suppose in entered word or copy pasted word contains new line then the word count is returning incorrect
following is the code please help out
override fun onTextChanged(s: CharSequence, start: Int, before: Int,
count: Int) {
val wordsLength: Int = countWords(s.toString()) // words.length;
limitmoderator.text = "$wordsLength/$MAX_WORDS"
val yourText: String =
moderator_intro.getText().toString().replace(160.toChar().toString(), " ")
if (yourText.split("\\s+".toRegex()).size > MAX_WORDS) {
var space = 0
var length = 0
for (i in 0 until yourText.length) {
if (yourText[i] == ' ') {
space++
if (space >= MAX_WORDS) {
length = i
break
}
}
}
if (length > 1) {
moderator_intro.getText()
.delete(length, yourText.length) // deleting last exceeded words
setCharLimit(moderator_intro, length - 1) //limit edit text
}
} else {
removeFilter(moderator_intro)
}}
private fun countWords(s: String): Int {
val trim = s.trim()
return if (trim.isEmpty()) 0 else trim.split("\\s+".toRegex()).size
// separate string around spaces
}
private var filter: InputFilter? = null
private fun setCharLimit(et: EditText, max: Int) {
filter = LengthFilter(max)
et.filters = arrayOf<InputFilter>(filter as LengthFilter)
}
private fun removeFilter(et: EditText) {
if (filter != null) {
et.filters = arrayOfNulls(0)
filter = null
}
}
so i have tried rplacing the "\n" in the text but it doesnt seems to be handling the case properly
any help will be appreciated
thanks in advance
Here's a different strategy than the one from the question you linked. Notice I'm using afterTextChanged and not onTextChanged!
I'm manually counting words to get the character index of the first whitespace after the last allowable word. That way I don't have to trim and then use Regex, and then try to figure out the index offset of that Regex. Then instead of applying a temporary filter, I directly cut the end of the Editable off.
editText.setSelection is to keep the cursor from jumping to the beginning.
override fun afterTextChanged(s: Editable) {
var previousWasWhitespace = true
var i = 0
var wordCount = 0
for (c in s) {
val whitespace = c.isWhitespace()
if (whitespace && !previousWasWhitespace && ++wordCount == MAX_WORDS) {
break
}
previousWasWhitespace = whitespace
i++
}
if (i < s.length) {
s.delete(i, s.length)
editText.setSelection(i)
}
}
You could write a regular expression to match the text that you want to keep and remove the rest. In this case you want match (non-whitespace+)(whitespace*) maximum 100 times.
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
val input = moderator_intro.getText().toString()
val maxWords = 100
val whiteSpace = "\\p{javaWhitespace}\u00A0\u2007\u202F" //All white space characters
val keepRegex = Regex("[$whiteSpace]*([^$whiteSpace]+[$whiteSpace]*){1,${maxWords.toString()}}")
val allowedText = keepRegex.find(input)?.value ?: ""
val wordAmount = allowedText.split(Regex("[$whiteSpace]+")).filter { it.isNotBlank() }.size
val trailingWhiteSpace = Regex("[$whiteSpace]+$")
if(wordAmount == maxWords && allowedText.contains(trailingWhiteSpace)) {
val newText = allowedText.replace(trailingWhiteSpace, "")
moderator_intro.getText().delete(allowedText.length, input.length)
setCharLimit(moderator_intro, newText.length)
} else {
removeFilter(moderator_intro)
}
}
I have a TextEdit that has the input type of numberDecimal and I executes some code with inputted number
when I enter a whole number it works fine but when I enter a number with a decimal point the app completely restarts
So how would I make it work with a decimal number?
if you do end up helping me thank you in advanced
KT file
class f_to_c : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.f_to_c)
val actionBar = supportActionBar
val calculate = findViewById<Button>(R.id.calculate)
val tempEntered = findViewById<TextView>(R.id.tempEntered)
val result = findViewById<TextView>(R.id.result)
val temp = findViewById<EditText>(R.id.temp)
if (actionBar != null){
actionBar.title = "Fahrenheit To Celsius"
actionBar.setDisplayHomeAsUpEnabled(true)
}
calculate.setOnClickListener {
var x = Integer.parseInt(temp.getText().toString()).toString().toInt()
tempEntered.text = x.toDouble().toString()
result.text = ((x-32).toFloat()*5/9).toString()
}
}
}
If you want to handle decimal numbers, you shouldn't use Integer.parseInt. That will throw an error on something like "1.23". The following should work (or you could use Double.parseDouble)
val x = temp.getText().toString().toDoubleOrNull() ?: 0.0
tempEntered.text = x.toString()
result.text = ((x-32.0)*5.0/9.0).toString()
You may want to handle the null case differently in case the user enters an invalid number like "1..3", but this would just default to 0 in that case instead of throwing.
Also, if this is to be used internationally, you should consider that some locales use a comma for a decimal separator ("1,23") - which will break here. You could either use a locale-aware number parser for that, or just replace commas with periods before converting.
val xs = temp.getText().toString().replace(",",".")
val x = xs.toDoubleOrNull() ?: 0.0
When i start my app, which should get the speed of the device, i get a NaN( i think its for " not a number"?) value for 1- 3 seconds in the textfield.The numbers are safed in a repository, but if is Nan, the app crashes.
How can i replace this NaN with 0(Zero)?... for ex.: If( NaNvalue = true){ (value = 0)}...something like that. I didnt find anything written in android or kotlin, just python(which i am not familiar with).
Any help would be great!!! Thank you...
my code:
fun getSpeed(location: Location): Float{
if (mRunning ) {
// val getkmh = (location.speed * 3.6)
// mSpeedKmH = getkmh.toFloat()
mElapsedSeconds = ((SystemClock.elapsedRealtime()
-mChrono!!.getBase()) / 1000)
mSpeedMeter = mDistanceTotal/mElapsedSeconds.toFloat()
mSpeed = ( mSpeedMeter *3.6).toFloat()// *3.6 > KmH
}
return mSpeedKmH
}
fun updateSpeed() {
val speedMeter = String.format(Locale.getDefault(), "%.1f",mSpeed)
tvSpeed.text = speedMeter
}
You can do it like the other answer shows or using takeIf and the elvis operator:
val speedMeter = String.format(Locale.getDefault(), "%.1f", mSpeed.takeIf { !it.isNaN() } ?: 0.0)
val value: Double = yourDoubleValue.let { if (it.isNaN()) 0.0 else it}
The same can be applied to Float values
Thanks to Fernando and Tenfour04!!! Both versions are working!!
val speedGesammt = String.format(Locale.getDefault(), "%.1f",
mSpeedAverageKmH.takeIf { !it.isNaN() } ?: 0.0)
val speedGesammt = String.format(Locale.getDefault(), "%.1f",
mSpeedAverageKmH.let { if (it.isNaN()) 0.0 else it})
I'm trying to build an age calculator application. When I didn't enter a number in one of the EditTexts and click on calculate Button, my app is crashed! "App has stopped" and click the photo
I tried to set yearbirth.isEmpty() || yearbirth.equals("") || yearbirth == null , but nothing worked.
CODE:-
class MainActivity : AppCompatActivity() {
var yearage:Int?= null
var monthage:Int?= null
var dayage:Int?= null
#SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buGetAge.setOnClickListener({
val year= Calendar.getInstance().get(Calendar.YEAR)
val month= Calendar.getInstance().get(Calendar.MONTH)
val day= Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
val monthage:Int?
val dayage:Int?
if (yearbirth== null) {
Toast.makeText(this, "You have to enter your year berth", Toast.LENGTH_LONG).show()
} else {
if (month < Integer.parseInt(monthbirth.text.toString())){
monthage=(Integer.parseInt(monthbirth.text.toString())- month- 12)* -1
}else if (month > Integer.parseInt(monthbirth.text.toString())) {
yearage=year- Integer.parseInt(yearbirth.text.toString())
monthage= (Integer.parseInt(monthbirth.text.toString())- month)* -1
}else {
yearage=year- Integer.parseInt(yearbirth.text.toString())
monthage=0
}
if (day < Integer.parseInt(daybirth.text.toString())){
dayage= (Integer.parseInt(daybirth.text.toString())- day- 30)* -1
}else if (day > Integer.parseInt(daybirth.text.toString())){
dayage= day- Integer.parseInt(daybirth.text.toString())
}else {
dayage=0
}
val a= (yearage* 12)+ monthage
val b= (a * 30)+ dayage
val ageinyears= "$yearage years"
val ageinmonths= "$a months"
val ageindays= "$b days"
txtshow.text = "Your age is $yearage years, $monthage months and $dayage days"
txtshow2.text = "\nYOUR AGE:\nin years:\n\nin months:\n\nin days:"
txtshow3.text = "\n\n$ageinyears \n\n$ageinmonths \n\n $ageindays "
this.yearage=yearage
this.monthage=monthage
this.dayage=dayage
}
})
}
}
Verify that yearbirth.text.toString() is not empty before attempting to parse it into an Integer (and also if it's a valid integer.)
To achieve so, put the line you showed in your code:
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
Inside an if verifying that the input is valid, like so:
if(yearbirth.text.toString().matches(Regex("\\d+")))
var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
You can check for empty Input like this:
newString = Integer.toString(yearbirth).trim();
if (newString.matches("")) {
Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show();
return;
}