Try this:
long startTime = System.currentTimeMillis(); //fetch starting time
boolean neededStatus;
do{
xmlFormat = driver.getPageSource();
neededStatus = xmlFormat.contains(“Needed”);
}while(!(neededStatus) && (((System.currentTimeMillis()-startTime) <= (5*1000))));
‘5’ is for 5 seconds to wait till the toast message is displayed. Generally standard timeout of a toast message is 2 seconds. You can keep this value around 2 to 5 seconds.
Let me know if this works !