Or symbol not working - android

I want to make the user to fill all the particular edittext and cannot leave it empty. I use .isEmpty code and the or symbol "|". This is what I make:
if(!e_name.getText().toString().isEmpty()|!e_numb.getText().toString().isEmpty()|!e_pangkat.getText().toString().isEmpty()
|!e_tarikh.getText().toString().isEmpty()|!e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}else {//another code}
when I run the code, even when the edit text is fill, it still toast "cannot empty" what did I do wrong?

Use double pipes for or ||, single pipes are for bitwise operations
Also your if-statement is probably wrong, currently you check if the strings are NOT empty and give an error.
You should remove the ! from the statements so you check if the strings ARE empty, then give the error.
if(e_name.getText().toString().isEmpty()||e_numb.getText().toString().isEmpty()||e_pangkat.getText().toString().isEmpty()||e_tarikh.getText().toString().isEmpty()||e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}
else {//another code}

Try like this
if(!e_name.getText().toString().isEmpty()||!e_numb.getText().toString().isEmpty()||!e_pangkat.getText().toString().isEmpty()
||!e_tarikh.getText().toString().isEmpty()||!e_masa.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(), "Cannot empty", Toast.LENGTH_SHORT).show();
return;
}else {//another code}

Related

Android, using Toast inside onClickListener

I am trying to make a toast display some text given certain conditions inside an onClickListener. The app won´t run in the simulator, and I get the following error: "void cannot be converted to Toast"
I have searched around, and find several similar problems and solutions on this forum, but none of them applies completely to my problem. The others haven´t used the correct context in the statement, but I really mean that I do. (The name of the javafile (context) is: "Case1Activity") Can anyone help me with this?
I have simplified the code a bit:
public void onClick(View view) {
if (button1Pushed == false){
count++;
Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
}
}
});
do it without assignment statement
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
apply it as.
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
if you want to use assignment operator then you can use below code
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Dear Friend check below before eating Toast,
Your Toast (Incompatible types error) :
Toast toast = Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
Normal Case (Standard Use):
Toast.makeText(Case1Activity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();`
Special Case (if you need reference of Toast):
View toast = Toast.makeText(MainActivity.this, "You are doing this in the right order!", Toast.LENGTH_LONG).show();
because here ".show()" method is -
public void show () which
shows the view for the specified duration.
Thanks

Check if editText of decimal number type is empty

I am confused for a long time about an issue. In an if statement I want to check that an editText with decimal input IS NOT empty. I found many answers to check if it IS empty but I want the opposite.
Here is my code:
if(r>20 && r<25){
startActivity(new Intent(Knees.this, HandJoints.class));
} else {
Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\nTry again", Toast.LENGTH_SHORT);
fail.show();
elbow_r.setText("");
}
break;
So in the first input statement I want to add an AND so as to prevent proceeding to the next activity in case that r field is empty. Besides this my application is running except the case I have no entry to that field.
Thank you in advance for reviewing my problem.
You can use this to check if a string is not empty:
if(!TextUtils.isEmpty(eblow_r.getText())){
//do something
}
I'm guessing elbow_r is the name of your EditText.
Simply adding a ! in front of anything will make it not that, that's some Java basics.
This is my whole override method
next1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double r = Double.parseDouble(elbow_r.getText().toString());
//startActivity(new Intent(Knees.this, HandJoints.class));
switch (v.getId()) {
case R.id.button2HandJoints:
if(r>20 && r<25 && !TextUtils.isEmpty(elbow_r.getText())){
startActivity(new Intent(Knees.this, HandJoints.class));
}else {
Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\n Try again", Toast.LENGTH_SHORT);
fail.show();
elbow_r.setText("");}
break;
default:
break;
}
}
});
Try something like this:
if(!TextUtils.isEmpty(elbow_r.getText().toString())){
double r = Double.parseDouble(elbow_r.getText().toString());
if(r>20 && r<25){
startActivity(new Intent(Knees.this, HandJoints.class)
} else {
Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\n Try again", Toast.LENGTH_SHORT);
fail.show();
elbow_r.setText("");
}
}
and don't forget to add android:inputType="number|numberDecimal" in your edittext xml

If - Else statement using the drawable of ImageView

Am trying to get the ImageView drawable and use it to create an if else statement, i have tried many solutions including the one below, all are not working they always give me the wrong Toast
if(holder.likeImage.getDrawable()==context.getResources().getDrawable(R.drawable.ic_action_like)){
Toast.makeText(context, "LIKED ALREADY", Toast.LENGTH_SHORT).show();
}
else if(holder.likeImage.getDrawable()!=context.getResources().getDrawable(R.drawable.ic_action_like)){
Toast.makeText(context, "YOU HAVNT LIKED", Toast.LENGTH_SHORT).show();
}
try this.
holder.likeImage.setImageResource(R.drawable.ic_action_like);
holder.likeImage.setTag(R.drawable.ic_action_like);
if(holder.likeImage.getTag().equals(R.drawable.ic_action_like)){
}else{
}

Toast in an AlertDialog in a try/catch

I am trying to have an error message when the use enters a number for an item that cannot be deleted in the database. I have gotten it so the user can delete an item from the database and I think used the try catch to avoid a run time error if the number does not exist. What I am trying to do is have a toast pop up when the user enter an invalid number. I have already looked at other similar postings on stack and have not have any luck. Here is my code. If you need me to post more code let me know.
public void onClick(DialogInterface dialog, int which) {
try {
String id=idNum.getText().toString();
long primaryId=Long.parseLong(id);
info.open();
info.deleteInspection(primaryId);
info.close();
dbInfo();
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "Number not found", Toast.LENGTH_SHORT).show();
}
}
You could make your deleteInspection() method return a boolean value, telling wether or not a given id was deleted. Then check for it in your onClick method:
boolean result = info.deleteInspection(primaryId);
if(!result) {
Toast.makeText(getApplicationContext(), "Number not found", Toast.LENGTH_SHORT).show();
}
It's not about try/catch here, that you want to post "number not found"
the thing you need is the return the result from your Database that it found this id to delete or not
As Soren.Qvist said.
you need to return some value from your Database to your code (maybe -1, false, etc.) and check at it

Toast.makeText from resource string

I have a class named MyPrimaryClass, this class has a button witch when pressed, creates an Intent with the class myClassForResult.
I use this to start it:
startActivityForResult(myIntentOfMyClassForResult, ACTIVITY_EDIT_BTEXT);
Both MyPrimaryClass, and myClassForResult extends Activity.
So, when I call Toast.makeText within the myClassForResult, with the text parameter of R.string.my_resource_string, it gives me Force Close!
I have tried this:
Context c = myClassForResult.this;
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
Also this: c = getApplicationContext()
Also this: c = getBaseContext()
Also this:
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_SHORT);
toast.show();
If I use an inline string, like "My toast Text!", it works. But i need to get a string from the resources.
-Problem solved:
To solve the problem I changed the duration of the Toast to Toast.LENGTH_LONG
The string R.string.my_resource_string value is "The title is empty"
When I change its value to "The title", it worked properly, so I guess the string was too long for the Toast.LENGTH_SHORT duration.
But when i change the duration to Toast.LENGTH_LONG, I could use the long string.
Context c = MyPrimaryClass.this;
Toast toast = Toast.makeText(c,
R.string.my_resource_string,
Toast.LENGTH_LONG);
toast.show();
One thing to note:
Toast toast = Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT);
toast.show();
Can be simplified into:
Toast.makeText(c,
c.getResources().getString(R.string.my_resource_string),
Toast.LENGTH_SHORT).show();
This saves you an object reference that you do not need.
One thing you need to understand is that whenever you reference you R in your package (not android.R.) you will have access to your resources as long as you have Context.
Update
After realizing what you are using this for I would recommend that you
change your approach, while this is in fact possible, your approach isn't ideal for something so simple.
The method startActivityForResult(xx) is typically when you want to start an application that is outside of your package for a result.
For instance: if I wanted to retrieve a barcode from a product, then I'd start an Intent to that barcode class, indirectly through an action. Then I'd retrieve the data through using onActivityResult(xx).
it makes No Sense to do this for your own classes.
Try:
Toast.makeText(this, this.getString(R.string.my_resource_string), Toast.LENGTH_SHORT);
#dilipkaklotar Answered correctly but a few changes needs to be done:
this is how it worked for me
Toast.makeText(getApplicationContext(),
getApplicationContext().getResources().getString(R.string.message),
Toast.LENGTH_SHORT).show();
the getResources has no parenthesis ().
and at the end is .show(); not toShow().
but it's correct. so thank you very much.
Toast.makeText(getApplicationContext(), getApplicationContext().getResources.getString(R.string.imgval), Toast.LENGTH_SHORT).toShow();

Categories

Resources