0

I want to show Toast when no results found after searching , I made a simple Toast but toast remains for long time and i want it to disappear after short time

this is code

            public boolean onQueryTextSubmit(String query) {

            final String[] dose = getResources().getStringArray(R.array.doses);
            final String[] use = getResources().getStringArray(R.array.uses);
            final String[] composition = getResources().getStringArray(R.array.composition);
            
            final TypedArray img  = getResources().obtainTypedArray(R.array.img);
            for (String search : suggestions)
            {
                if (query.equals(search)){

                    for (int i =0; i<suggestions.length; i++){

                        if (query.equals(suggestions[i])){
                            Intent intent = new Intent(MainActivity.this,productDetails.class);

                            int im = img.getResourceId(i,-1);

                            intent.putExtra("img",im);

                            intent.putExtra("dose",dose[i]);
                            intent.putExtra("use",use[i]);
                            intent.putExtra("composition",composition[i]);
                            intent.putExtra("title",suggestions[i].toUpperCase());

                            startActivity(intent);
                        }
                        }
                }
              else if (query!= search){
                    Toast.makeText(MainActivity.this, "No Results Found", Toast.LENGTH_SHORT).show();
                    
                }

            }

            return false;
        }
  • https://stackoverflow.com/q/2220560/1499877 you cannot define a custom duration for Toast.makeText. `Toast. LENGTH_SHORT` is the `0` flag, and `Toast. LENGTH_LONG` is the `1` flag. – WOUNDEDStevenJones Apr 22 '21 at 15:28
  • I would recommend trying to indent your code properly, then it would make it much easier to see what's going on. – Henry Twist Apr 22 '21 at 15:29

1 Answers1

1

Your toast is showing in for loop, he is added to queue multiple time, remove it from loop