I would like to dynamically change the content of a view in the middle of a function executing. Specifically I have a function that records audio from the user. This will be called multiple times (lets say 2). I want to have it so that the user only pushes the record button once and the view changes to guide the user through the process so for instance:
- User presses record Button.
- TextView1 appears and says "Record audio part 1".
- Record function is called.
- TextView1 changes so that it says "Audio part 1 recorded successfully".
- Underneath TextView1 a new TextView2 pops up saying "Record Audio part 2"
- Record function is called again.
- TextView2 changes and now says "Audio Part2 Recorded successfully"
I have tried implementing this with the setContentView() method but the view does not change to the user they are simply met with the view that should appear at the end (i.e. Part 1 recorded successfully, Part2 recorded Successfully). From what I've read I understand that this happens because setContentView() doesn't draw the view to the screen until all functions are finished executing.
There must be a way of updating the view dynamically in android but after hours of searching I cant quite find what I'm looking for. Any suggestions would be greatly appreciated.
EDIT:
I have already tried set text but it doesnt work as described above. setText changes the content of a text view, but it only does it after all other operations have finished so if I have:
Function(View view)
{
TextView tv = findViewById(R.Id.textView1);
tv.setText("Before");
record();//records two seconds of audio
tv.setText("After")
}
With the above code the user won't see "Before" in the text view. They will only see "After" as it will only draw to the screen when the entire function is finished executing.
EDIT 2: I have found the solution after a couple of days of grinding. I am using the AsyncTask in android. I was unaware of this class before hence why I didn't know how to do it. For anyone with a similar problem here is my solution that I have working.
//In Activity
public void whenButtonPushed(View view)
{
new RecordAsyncTask(this,RecordAudio.this).execute();
TextView tv = (TextView)findViewById(R.id.TextView1);
tv.setText("Before"); // Before appears on screen to user and is later replaced with after from within the AsyncTask
}
When I am calling the AsyncTask to I am passing it parameters of a context and an activity. This is so when it is finished carrying out its task it can update the screen as desired. I designed my AsyncTask class based on this Refresh Game Score TextView using AsyncTask