mahmoud elmahgoub
mahmoud elmahgoub

Reputation: 57

Toast message remain for long time

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;
        }

Upvotes: 0

Views: 56

Answers (1)

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

Upvotes: 1

Related Questions