Kylie Moden
Kylie Moden

Reputation: 1142

Why won't my TextView update/reset in android?

I have created a TextView, public TextView textView;, which I later define in my MainActivity.java in onCreate:

    textView = (TextView) findViewById(R.id.textViewName);

However, when I do set the text, it is not updating properly. Whenever I use the setText method, it does not update on the screen. The initial call to setText is in in a method called recordClap().

    /**set text view*/
    textView.setText("listening...");

This text is not updated to the screen.

Lastly, I set the text to display "Success!" once certain conditions have been met.

    textView.setText("Success!");

For some reason this is the only call to 'setText' that works.

So, why is the TextView not updating the new text properly? Is there something I have left out?

Full code below:

public class MainActivity extends Activity{

private static final String TAG = "Clapper";
private static final long DEFAULT_CLIP_TIME = 1000;
private long clipTime = DEFAULT_CLIP_TIME;

/**create text view*/
public TextView textView;
private boolean continueRecording;
public static final int AMPLITUDE_DIFF_LOW = 10000;
public static final int AMPLITUDE_DIFF_MED = 18000;
public static final int AMPLITUDE_DIFF_HIGH = 32767; 
private int amplitudeThreshold=AMPLITUDE_DIFF_HIGH;
private MediaRecorder recorder = null;
private static String tmpAudioFile = null;

public boolean recordClap()
{
/**set text view*/
textView.setText("listening...");

Log.i(TAG, "record clap");
boolean clapDetected = false;

try
{

    tmpAudioFile = Environment.getExternalStorageDirectory().getAbsolutePath();
    tmpAudioFile += "/audiorecordtest.3gp";

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(tmpAudioFile);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.prepare();

    Log.i(TAG, "i've been prepared!");
}
catch (IOException io)
{
    Log.e(TAG, "failed to prepare recorder ", io);
}

recorder.start();
int startAmplitude = recorder.getMaxAmplitude();
Log.i(TAG, "starting amplitude: " + startAmplitude);

do
{
    Log.i(TAG, "waiting while recording...");
    waitSome();
    int finishAmplitude = recorder.getMaxAmplitude();
    int ampDifference = finishAmplitude - startAmplitude;
    if (ampDifference >= amplitudeThreshold)
    {
        Log.w(TAG, "heard a clap!");
        /**here is the output to screen*/
        /**reset text view*/
        textView.setText("Success!");

        clapDetected = true;
    }
    Log.d(TAG, "finishing amplitude: " + finishAmplitude + " diff: "
            + ampDifference);
} while (continueRecording || !clapDetected);

Log.i(TAG, "stopped recording");
done();

return clapDetected;
}

private void waitSome()
{
try
{
    // wait a while
    Thread.sleep(clipTime);
} catch (InterruptedException e)
{
    Log.i(TAG, "interrupted");
}
}

public void done()
{
Log.d(TAG, "stop recording");
if (recorder != null)
{
    if (isRecording())
    {
        stopRecording();
    }
        //now stop the media player
        recorder.stop();
        recorder.release();
    }
}

public boolean isRecording()
{
    return continueRecording;
}

public void stopRecording()
{
    continueRecording = false;
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("hello", "world!");
    setContentView(R.layout.activity_main);

    /**define text view*/
    textView = (TextView) findViewById(R.id.textViewName);

    /**run*/
    recordClap();

/**Restart Button*/    
final Button button = (Button) findViewById(R.id.button_id);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
        recordClap();
    }
  });
}
} 

Upvotes: 1

Views: 1562

Answers (2)

Jack
Jack

Reputation: 9242

As I said in my comments, the change is happening so fast you are not seeing it. Try slowing it down, or increasing your waitSome possibly? But watch out - if I remember correctly in your waitSome() method, you are Thread.sleeping on the UI thread - which will cause the UI not to update during this time. So it may be happening like this (I can't remember exactly how the UI thread queueing works):

  1. recordClap is called
  2. textView.setText("listening") is called and put on the queue (but UI not updated yet)
  3. Thread.sleep(1000) pauses UI thread
  4. TextView says "listening" for a split second then moves on

Upvotes: 1

Akhmas Meraj
Akhmas Meraj

Reputation: 1

Kindly take a reference of textview i.e textView = (TextView) findViewById(R.id.textViewName); in recordclap() function before you set the text.

Thanks

Upvotes: 0

Related Questions