Else If textfield (Android) - java

I am creating a else if statement for a textfield i have on my view, i am using this code right now. But i'm running into a few errors
private EditText editText;
editText = findViewById(R.id.your_custom_id);
//then something like this
Sting text = editText.getText();
if(text.equals("foo") {
// do something
else if(text.equals("bar") {
// do something else
} else {
// something else
}
The errors consist of "Can't resolve symbol for 'sting' or 'equals'. 'Unknown class "editText" in the 'editText = findViewbyid...'. And the last one is 'Else without if' on the "else if(text.equals("bar")" code. How can i resolve this?

See Sting text = editText.getText(); This is totally incorrect.
There is no such thing as Sting it's String.Equal method apply on object types and String is an Object.
Also you need to use toString().
Use String text = editText.getText().toString();
you need to use editText = (EditText)findViewById(R.id.your_custom_id);
instead of editText = findViewById(R.id.your_custom_id);.

Related

Verify that at least one of the three TextView is written

hi everyone I hello everyone I have an application
with three TextView in a fragment and I want at least one of the three TextView is never empty, that is, at least one of the three extview must always be written.
My code is:
TextView comment_text = getActivity().findViewById(R.id.addtext_comment);
TextView mymenu = getActivity().findViewById(R.id.mymenu);
TextView mymenu2 = getActivity().findViewById(R.id.mymenu2);
String comment = String.valueOf(comment_text.getText());
String mymenu_text= String.valueOf(mymenu.getText());
String mymenu2_text= String.valueOf(mymenu2.getText());
if (validate(mymenu_text) && validate(mymenu2_text) && comment.length()==0||
validate(mymenu_text) || validate(mymenu2_text)||comment.length()!=0) {
Log.d("main_activity_spinner", "it's ok");
}
public boolean validate(String text) {
if (text.contains("null")) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Attenction!");
builder.setMessage("please insert at leat comment or delat or status");
builder.show();
return false;
}else
return true;
}
Now my code, if I write in one of the three textView, it returns the alert "please insert at least comment or delay or status" and then Log("it's ok"),but I want that if I don't write in any of the TextView, I get back an alert and if I write in one of the three TextView (or if I write in two or in all three TextView), I return the Log "it's ok". How can i do?
To check that at least one of them has text, you just have to check whether all the strings are empty. If so, show your warning, and if not, "it's ok":
EditText comment_text = getActivity().findViewById(R.id.addtext_comment);
EditText mymenu = getActivity().findViewById(R.id.mymenu);
EditText mymenu2 = getActivity().findViewById(R.id.mymenu2);
String comment = comment_text.getText().toString();
String mymenu_text= mymenu.getText().toString();
String mymenu2_text= mymenu2.getText().toString();
// if all three strings are empty, show the alert, otherwise log "it's ok"
if( comment.isEmpty() && mymenu_text.isEmpty() && mymenu2_text.isEmpty() ) {
// all of them are empty
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Attenction!");
builder.setMessage("please insert at leat comment or delat or status");
builder.show();
}
else {
// at least one has text
Log.d("main_activity_spinner", "it's ok");
}
Note 1: I assume these are actually EditText views, not pure TextView instances, since TextView is not editable by a user at runtime.
Note 2: For this to work, you have to call it at the time of validation. If you put this in something like onCreate it will be meaningless since the user wouldn't have had time to enter anything anyway.

Android Studio getText() and setText() not working with error: "can't resolve method "getText/setText"()"

So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().
setText() was for writing the output below is what I used:
(TextView)findViewById(R.id.outView.setText(textoutput));
getText() was for reading the input text then writing it to a variable and below is what I used:
String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();
You're chaining the method at the wrong spot. Remove .getText() from R.id.inText and place it after the brackets like (same thing for TextView):
String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();
Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:
EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();
You set the getText() and setText() method in wrong place.
getText() and setText() are methods of TextView and EditText classes.
But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.
You can do the following.
((TextView) findViewById(R.id.outView)).setText(textoutput);
and
String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();
It's not working since you need to first set the TextView:
TextView tvOut = (TextView) findViewById(R.id.outView);
TextView tvIn = (TextView) findViewById(R.id.inText);
String out = tvOut.getText().toString();
String in = tvIn.setText(out);
If you use a field check the compiler didn't automatically set it as a view instead of EditText type.
Also the casts are redundant now so you better not use them if you don't have to

EditText text cannot be converted to int as there is no text there

Basically I need to check that the user input from inputET (an EditText) is equal to the integer, correctAnswer. The problem I'm getting is that "" (which is the text in the EditText field) cannot be converted to an int. Is there any other ways of achieving this or catching the error, I've tried the following code which to my understanding asks if the string in the EditText is not equal to "". Am i going the right way about this or is there an easier way?
// check the input
if (inputET.getText().toString() != "") {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
if the user inputs the same int as the correctAnswer integer then the EditText text is reset to "".
Thanks for any help.
try this:
if (!inputET.getText().toString().equals("")) {
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
Used .equals() method for String comparison.
Based on your requirement I think using TextUtil class will be right way to go for checking the edittext is empty or not.
if(!TextUtils.isEmpty( inputET.getText().toString())){
if (correctAnswer == Integer.parseInt(inputET.getText()
.toString())) {
inputET.setText("");
newSum();
}
}
rather tha doing if (inputET.getText().toString() != "") have a try with
if (!inputET.getText().toString().equals(""))
print the "inputET.getText().toString()" to console and see what it returns. I would hope you need to check the following
String str = inputET.getText().toString()
if (null!=str && str.trim().equals("")) {
if(inputET.getText().toString()!=null&&!(inputET.getText().toString().isEmpty())){
//code for mot null
}else{
//code for null
}

Android NumberFormatException when parsing EditText

I'm writing an app that takes in an input from the AddBook class, allows it to be displayed in a List, and then allows the user to Search for their book. To this end, I'm creating a temporary EditText, binding it to the box where the user actually enters their search value, then (after ensuring that it is not empty) I compare what they've entered for the ISBN number with the ISBN numbers of each entry in the arrayList of <Book> custom objects, the list being named books.
Problem is, when I try to parse the EditText into an Int, it doesn't seem to work. I first tried using toString() on the EditText, then using Integer.parseInt() on the result of that method, but it hasn't worked out, as the conversion is seemingly unsuccessful;
All of the resources are in place and the code compiles properly, so those aren't the problems here.
EditText myEdTx = (EditText) findViewById(R.id.bookName);
if(myEdTx.getText().equals("")){Toast toast = Toast.makeText(this, "Please enter something for us to work with!", Toast.LENGTH_SHORT);
toast.show();}
else{
//resume here
for(int i=0; i<books.size(); i++)
{
Book tBook = new Book(i, null, null); tBook = books.get(i); String s=myEdTx.toString();
int tInt = Integer.parseInt(s);`
To get the string representation of an EditText's content, use:
String s = myEdTx.getText().toString();
Using toString() directly on the EditText gives you something like:
android.widget.EditText{40d31450 VFED..CL ......I. 0,0-0,0}
...which is clearly not what you want here.
You assume the user inputs a number into the text field, but that is unsafe, as you only get a string text (which theoretically can contain non-numbers as well). When I remember correctly, you can adjust a text field in android where a user only can input numbers, which should suit you more.
NumberFormatException occurs when Integer.parse() is unable to parse a String as integer, so, its better to Handle this exception.
String s = myEdTx.getText().toString();
try {
int tInt = Integer.parseInt(s);
} catch( NumberFormatException ex ) {
//do something if s is not a number, maybe defining a default value.
int tInt = 0;
}
So the current String here you are trying to parse is with white space in the line
and integer class unable to parse that white space. So use following code.
String s=myEdTx.getText().toString();
int tInt = Integer.parseInt(s.trim());
String s = myEdtx.getText().toString().trim();
int iInt = Integer.parseInt(s);

if statement problems in android

I have a method that checks for a null value from an editText on a click of a button like so:
public void myClickHandler09(View chv){
if (text9.equals("")){
text9.setText("0");
}else{
converter(text9);
}}
The
converter(text9);
method is as shown:
public void converter(View view){
switch (view.getId()) {
case R.id.Button09:
RadioButton RadioButtons = (RadioButton) findViewById (R.id.RadioButton901);
float inputValue = Float.parseFloat(text9.getText().toString());
if (RadioButtons.isChecked()) {
text9.setText(String
.valueOf(convertRadioButtons(inputValue)));
}
break;
}}
private double convertRadiobuttons(float inputValue){
return inputValue * 6.4516;
}
The method is larger but here i've only called one radiobutton to shorten it.
Right now though the if statement seems to do absolutely nothing and so non of the rest of the code works. If i remove the method and rename
converter(View view){
to
myClickHandler09(View view){
then the code works and until you enter a null value into the EditText (then it crashes)
What am I doing wrong exactly here?
NOTE: the method name "myClickHandler09" is linked to the button as android:onClick in the xml
You need to do if("".equals(text9.getText().toString())) { ...
The toString() is there because the TextView will return a CharSequence which may or may not be a String.
Right now you are comparing the TextView itself to "", and not the String it is showing.
Edit - As far as the crash goes, you also want to catch the NumberFormatException that Float.parseFloat() throws.
float inputValue = 1.0f; // some default value, in case the user input is bad.
try {
inputValue = Float.parseFloat(text9.getText().toString());
} catch (NumberFormatException e) {
// possibly display a red flag next to the field
}
Why not try
if ("".equals(text9.getText())) {
} else {
}
You essentially have to do a getText() from a TextView and not equals a String with a TextView.
One thing I don't understand with your code is that you call:
converter(text9);
passing in the EditText, but by replacing converter(View view) with the function name myClickHandler09 (like so):
myClickHandler09(View view) {
the button being pressed with call this function (if you defined it in the xml layout onClick paramter).
So to match this behaviour with your current code, try this out:
public void myClickHandler09(View btnView){
if (text9.equals("")){
text9.setText("0");
} else {
converter(btnView);
}
}
I may have missed the point of you're post, but I think that is part of your issue. Also in stead of .equals("") I prefer (text9.toString().length() > 0) just seems a bit more logical, but that's me being a bit pedantic.

Categories

Resources