Check value in text view - android

I have a if condition for check value in textview but it still happen only else condition
what I did wrong, why it doesn't have if condition even if I have set lang=space
private void setLangTitle() {
lang.setText(" ");
db.open();
Cursor cc = db.getLangAct();
cc.moveToFirst();
int index = cc.getColumnIndex(DBAdapter.KEY_LANG);
while (cc.isAfterLast() == false) {
if(lang.equals(" ")){
lang.append("p"+cc.getString(index));
cc.moveToNext();
}
else {
lang.append("/" + cc.getString(index));
cc.moveToNext();
}
}
db.close();
}

You're currently checking if lang is equal to a space character. If lang is a TextView, than it will not be equal to the space character because it is a TextView object, not a string. You probably want to test if the text being displayed on lang is equal to the space character, which would be something along the lines of:
if (lang.getText().toString().equals(" ")) {
...
}

I assume that lang is your TextView object? Then you should use lang.getText().toString()

you should use
if(lang.getText()equals(" ")){
...
}
You were not comparing texts, you were comparing objects. And, of course, a TextView doesn't (shouldn't) compare equal to a String.

Related

how to check the text in first two buttons equals to the text in third button

Here is my code .This doesn't work
button1.getText() + button2.getText() == button3.getText()
Use equals to compare strings,
Try this,
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if((button1Text + button2Text).equals(button3Text)){
// strings are equal
} else {
// strings are not equal
}
button[6].getText().toString().equals(button[0].getText().toString().concat(button[3].getText().toString()));
When you work with Strings in Java the operator == checks if the two objects refer to the same instance of an object.
While equals() checks if the two objects are actually equivalent, even if they are not the same instance.
try:
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if (button1Text.equals(button3) && button2Text.equals(button3)) {
// do something...
} else {
// do something...
}

How to ignore spaces between text editview Android

I am trying to ignore spaces in editview between text, I am not quite sure how I can go about doing this. I know I can use trim feature to ignore spaces before and after the full text but how do I ignore space between strings if there is any;
String myTextEdited myText.getText().toString().trim();
For example, if I have / user types in this;
Allan Bob
3523 JKO
NY1 U90
I want to ingore spaces when I read this in my if statement or put it in another variable for example;
String name = "AllanBob"
For example, to ignore upper and lower cases I am doing this;
if (myText.getText().toString().trim().equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
What I would like to do is add another feature in here that also ignores spaces before, between and after text e.g. instead of;
myname is Henry . (space until here)
It should read it as mynameishenry but to the user it still appears as they have written it.
Please let me know if my question was not clear, I will try explaining it better
EDITED:
is it possible to ignore spaces in string that I have inside my if statement. For example;
if (myText.getText().toString().trim().equalsIgnoreCase("Henry 0887")) {
// do something
} else {
// do something
}
but currently if the user types in henry0887, the if statement does not validate it because I added a space inside my validation text and therefoe its looking for a space in the text, is it possible to over come this, so even if I have space inside my validation it ignores it.
Did you try this:
String myString = myEditText.getText().toString();
myString = myString .replace(" ", "");
Hope it helps
EDIT:
if (myText.getText().toString().replace(" ", "").equalsIgnoreCase(userInput) || myText.getText().toString().equalsIgnoreCase(userInput)) {...
Try this,
if(myText.getText().toString().trim().replace(" ","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
Hope this helps.
use replaceAll() method.
str = str.replace(" ","");
or for all space chars:
str = str.replace("\\s+","");
EDIT
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase(userInput)) {
// do something
} else {
// do something
}
EDIT2
if (myText.getText().toString().replace("\\s+","").equalsIgnoreCase("Henry 0887".replace("\\s+",""))) {
// do something
} else {
// do something
}

Android app crashes when no value is in edit text

I am making a unit converter, but if I do not enter any value into edit text and press the calculate button the app crashes with error Invalid float: "". Also, I want to forbid zeroes from being entered before numbers (eg. 0300). How do I accomplish this?
//handle calculate
calcButton=(Button)findViewById(R.id.calcButton);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Spinner spinner = (Spinner) findViewById(R.id.unit_spinner);
String spinnerText = spinner.getSelectedItem().toString();
EditText unit_edit = (EditText) findViewById(R.id.unit_edit);
amount = Float.valueOf(unit_edit.getText().toString());
if (unit_edit.getText().toString().equals(null)) {
Toast.makeText(getApplicationContext(), "Insert Value To Convert",
Toast.LENGTH_LONG).show();
} else {
switch (spinnerText) {
case "Kilograms":
kilograms = amount;
grams = amount * 1000;
ListView();
break;
case "Grams":
grams = amount;
kilograms = amount / 1000;
ListView();
break;
}
}
}
});
}
You are probably getting an NumberFormatException thrown since the EditText fields text is "" and "" is not a valid float value, the exception is thrown at the following line:
amount = Float.valueOf(unit_edit.getText().toString());
What you'll need to do is add some validation and checking before trying to get the float value of a String.
Check the methods documentation for more details http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#valueOf(java.lang.String)
This might be useful for your EditText to limit input to numbers only.
<EditText
android:id="#+id/unit_edit"
android:inputType="number"
/>
You can also limit the digits, type of number such as decimal
<EditText
android:id="#+id/unit_edit"
android:digits="0123456789."
android:inputType="numberDecimal"
/>
You can't parse an empty value to float. You should first test if it's empty, and then do what you want, something like this:
String text = unit_edit.getText().toString();
if(!text.isEmpty()){ // Test if the text is empty
if(text.matches("[0-9]+")){ // Test if it only contains numbers, using REGEX
amount = Float.valueOf(text); // Only then parse to float.
// Switch and rest of the stuff
} else {
Toast.makeText(getApplicationContext(), "Use only numbers from 0 to 9.",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "The field is empty",
Toast.LENGTH_LONG).show();
}
The comments explain what's going on. About the leading 0 in some numbers, using "valueOf" will remove it already, and 0300 will be parsed as 300, so there's nothing to worry about.If you still want something related to it, let me know and i'll edit my answer.

android Cannot invoke substring(int, int) on the primitive type long

I have textview which will show large numbers and if the number of digits is larger than 5 I want to make the textview show only 4 digits with dots like this(3544...) I tried but I got this error:
android Cannot invoke substring(int, int) on the primitive type long
here is my code:
EditText EditNumber;
long theNumber;
String str = EditNumber.getText().toString();
theNumber = Long.parseLong(str );
if( theNumber >5)
{
theNumber = theNumber.substring(0,4)+"..."; // the error in this line.
textView1.setText(Long.toString(theSide));
}
else
{
textView1.setText(Long.toString(theNumber));
}
As pointed by Martin Cazares the long does not have substring. Use your string instead of your double value.
EditText EditNumber;
long theNumber;
String str = EditNumber.getText().toString();
if( str.length() > 4) // > 4 digits
{
textView1.setText(str.substring(0,4)+"...");
}
else
{
textView1.setText(str);
}
Hope it helps!
Long.toString(theNumber).substring(x,y);
That should give you the digits you want.
You're trying to do a substring method call on a Long which doesn't have that method. You probably intended to do str.substring(0, 0) instead.
The error is that "long" do not have a substring method, be carefull with primitives, they do not have any methods at all...
If you want to substring do it like this:
String theNumber = str.substring(0,4)+"...";
textView1.setText(theNumber);
But beyond that, you might not even need to do it your self, look at the
android:ellipsis="end" property of the TextView
it will do the ellipsis for you if the size of the TextView is smaller than the actual text.
Regards!

android: check if the string not only white spaces

How can I check if a string contains anything other than whitespace?
This code didn't work:
String string = " \n\n\t\t ";
if(string.length()==0) doSomething();
since spaces and new lines have values.
Can anyone tell me how can I do it?
Note: minSDKVersion = 5
Regards :)
Try this:
if (string.trim().length() == 0) { /* all white space */ }
Alternatively, you can use a regular expression:
if (string.matches("\\w*")) { . . . }
try:
if (string == null || TextUtils.isEmpty(string.trim()) doSomething();
You can use trim to remove whitespace from both ends of the string. Then compare with the empty string.
Kotlin:
The below code takes the string, trims all of the letters down, and checks to see if the result is white space by using .isEmpty().
val str = " "
if (str.trim().isEmpty()) {
// white space only
} else {
// this string has actual characters/letters in it
}
Try: it works
if (txt.getText().toString().trim().matches(" ")){
Toast.makeText(getApplicationContext(), "You did not select any text"
, Toast.LENGTH_LONG).show();
}
else{
}

Categories

Resources