I think the problem is that you are running the while loop in the same thread your listeners and your UI is running in. Thus, as soon as your program enters mainprogram(), it will hang up in this loop, because it can no longer react to your UI (thus, perform will always be true
)!
The problem you are facing might be solved with running the content of mainprogram()
in another Thread.
Your code might look something like this:
class MainActivity extends Activity
{
//...
private static boolean perform;
private static getPerform()
{
return perform;
}
public void onClick (View v){
switch(v.getId()) {
case R.id.button1:
perform = true;
Thread t = new SpeakThread();
t.start();
break;
case R.id.button2:
perform = false;
break;
}
}
}
class SpeakThread extends Thread
{
public void run()
{
while(MainActivity.getPerform())
{
speak();
}
}
}