how to check if the toast have dismissed or not [duplicate] - android

This question already has answers here:
Stop all toast messages when going to another screen in Android
(3 answers)
Closed 8 years ago.
i want to check if the toast have dismissed or not ,because user click the mouse the toast is show,but may me the user continuous click,so i need to check,i cannot use the dialog

Toast toast = null;
if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
toast = Toast.makeText(getApplicationContext(),
"Text", Toast.LENGTH_SHORT);
toast.show();
}
Check if the toast is visible before you show it again.

Toast toast = yourToastCreationCode();
if (null == toast.getView().getWindowToken())
{
yeahToastIsInvisible();
}

Based on Denis answer, but worked better for me.
Toast t;
t=Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG);
t.show;
if (t.getView().isShown())
{
//visible
}

Related

Java if statement not working on a condition of a service [duplicate]

This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I have created an app in android studio and it uses a background service which has a method which starts a counter of seconds. I have created an if statement to test if the seconds are at 0 and when i debug the value seems to be correct but the condition is jumping straight to the else statement and i can not figure out why.
public void checkService(){
long secs = seconds;
String str = String.valueOf(secs);
if(str == "0")
{
Toast.makeText(this, "Not started", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
}
}
It is jumping to the second esle even when the value is 0. The image shows the value of str when i was debugging.
You should never test objects with ==. You should use .equals instead:
if(str.equals("0"))
{
Toast.makeText(this, "Not started", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
}
Some explanation at an answer from another post: https://stackoverflow.com/a/513839/2174489

validate editText on buttonclick event [duplicate]

This question already has answers here:
Form validation on Button click in android
(3 answers)
Closed 5 years ago.
I am new in android programming and still learning. for practice i started to create a very simple and basic app which calculate age on button click which take birth year as an input.
i manage to get it working and want to do some validation on it. like i have done validating that the value we provide should not be more than current year and i successfully done that.
Now my problem is that i also want to put validation that if no input is been made and button is pressed then it should alert the user by Toast. So far i have tried doing but every time i press button without anything in editText the app just crash.
here's my code.
calcAge.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dob > year){
Toast.makeText(getApplicationContext(), "Provide valid Input", Toast.LENGTH_SHORT).show();
}
else if(String.valueOf(dob) == null){
Toast.makeText(getApplicationContext(), "Please Provide Some Input", Toast.LENGTH_SHORT).show();
}
else{
dob = Integer.parseInt(String.valueOf(inAge.getText()));
currentYear.setText("Your Curret Year is :- " + year);
yourYear.setText("Your Birth Year is :- "+ dob);
int a = year-dob;
finalAns.setText("Your Age is :- " + a);
inAge.setText("");
}
}
});
above code is of button click. Please provide with some solution
I have also tried putting editText1.getText().toString.trim().lenght()==0 in if condition but that also didn't worked and app just crashes.
try this on button click listner
calcAge.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (yourEdittext.getText().toString().equals("")) {
yourEdittext.setError(getString("filed can't be empty"));
yourEdittext.requestFocus();
Toast.makeText(getApplicationContext(), "Please Provide Some Input", Toast.LENGTH_SHORT).show();
}
}
});
Use this:
String dob = yourEditText.getText().toString();
then check it like this:
if (TextUtils.isEmpty(dob)) {
//show toast
}
You have to do validation like this :
doneDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(validationDetails()){
//If get all data do Action
}else{
// Toast.makeText(getApplicationContext(),"Field Missing",Toast.LENGTH_SHORT).show();
}
}
This is the validation part method:
private boolean validationDetails() {
if (userNameET.getText().toString().isEmpty()) {
userNameET.setError("Enter Patient Name");
return false;
}
if (ageET.getText().toString().isEmpty()) {
ageET.setError("Enter Age ");
return false;
}
if (gender.getSelectedItem().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
On button click, you are checking first if dob is greater than a year. which is not correct. First, check if dob is null or empty. then check for if greater than year
If (dob == null && dob.isEmplty){
Toast.makeText(getApplicationContext(), "Please Provide Some Input", Toast.LENGTH_SHORT).show();
} else if(dob > year){
Toast.makeText(getApplicationContext(), "Provide valid Input", Toast.LENGTH_SHORT).show();
} else {
//do what ever you want
}
try this on your button click :
if ( yourEditText.getText().toString().equal("")) {
Toast.makeText(getApplicationContext(), "Please Give Some Input", Toast.LENGTH_SHORT).show();
}

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

Toast messages being queued?

When my submit button is clicked, it calls a method which checks if a certain EditText is empty and displays a toast message as an error message accordingly. However, if I rapidly tap on submit, it "queues" a lot of toast messages. How can I prevent this?
Here's my method:
private void checkName() {
if (etName.getText().toString().isEmpty()) {
Toast toast = Toast.makeText(this, "Please enter your name", Toast.LENGTH_LONG);
toast.show();
} else {
submit();
}
}
What happens is you are creating new toasts each time checkName() is called, hence they are "queued" by the system and shown one after another. You can try the following to ensure you are just making one toast and simply show it when needed:
Toast mToast;
private void checkName() {
if (etName.getText().toString().isEmpty()) {
if (mToast == null) { // Initialize toast if needed
mToast = Toast.makeText(this, "", Toast.LENGTH_LONG);
}
mToast.setText("Please enter your name"); // Simply set the text of the toast
mToast.show(); // Show it, or just refresh the duration if it's already shown
} else {
submit();
}
}

Using a != statement with an edittext object in java?

Im trying to create a button which when pushed reads the edittext box to
Make sure its not blank
Make sure its not the default text in this case "First Name".
However when the button is pushed it still preforms the action even if the edittext text is First Name or blank. Is there an easier way to do this? Also the toast are not made when the text is First Name or blank.
createp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (fname.getText().toString() != "")
if (fname.getText().toString() != "First Name"){
prefsEditor.putInt("user", 1);
prefsEditor.commit();
}
if (fname.getText().toString() == "")
{
Toast.makeText(createactivity.this, "You need a first name to create a profile!",
Toast.LENGTH_LONG).show();
}
if (fname.getText().toString() == "First Name") {
Toast.makeText(createactivity.this, "You need a first name to create a profile!",
Toast.LENGTH_LONG).show();
}
}});
}
Try this instead:
if (!fname.getText().toString().equals(""))
...
Another example:
if (fname.getText().toString().equals("First Name"))
....

Categories

Resources