0

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:

  1. User presses record Button.
  2. TextView1 appears and says "Record audio part 1".
  3. Record function is called.
  4. TextView1 changes so that it says "Audio part 1 recorded successfully".
  5. Underneath TextView1 a new TextView2 pops up saying "Record Audio part 2"
  6. Record function is called again.
  7. 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

Community
  • 1
  • 1
IanSoc
  • 238
  • 1
  • 3
  • 14
  • In this situation it is better to use Fragments, read more about them here: http://developer.android.com/guide/components/fragments.html – Waqas Mar 06 '13 at 12:10

1 Answers1

0

As you want to put the text progrematicly you should apply it in you code and not in the XML layout file.

So you have to create a TextView in you layout, find it in you code by:

TextView tv = (TextView)findViewById(R.id.your_text_view_id);

and then, during the execution of you function change it text accordingly by this command:

tv.setText("desired text");
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • So you don't need to re-add the TextView tv? The TextView on screen will automatically update itself even though the function that contains tv.setText("whatever") is still carrying out other operations? – IanSoc Mar 06 '13 at 12:49
  • I didn't get you qustion? what do you mean by carrying out other operations? set the setText method at the location that you want it to happen. – Emil Adz Mar 06 '13 at 13:03
  • from your edit, your TextView updates should be performed from within the record() method so you can see updates as this method runs along. – Emil Adz Mar 06 '13 at 14:41
  • I have tried this with the same results as described. Setting the text to "Before" Is never seen, the user only sees "After". – IanSoc Mar 06 '13 at 14:48
  • My code is as I described. I think you have misunderstood the problem or misunderstood how the setText method works with views. – IanSoc Mar 07 '13 at 10:20
  • maybe you right... and I really misunderstood you problem. I just trying to help. good luck. – Emil Adz Mar 07 '13 at 12:28