Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't my TextView update/reset in android?
    primarykey
    data
    text
    <p>I have created a TextView, <code>public TextView textView;</code>, which I later define in my MainActivity.java in <code>onCreate</code>:</p> <pre><code> textView = (TextView) findViewById(R.id.textViewName); </code></pre> <p>However, when I do set the text, it is not updating properly. Whenever I use the <code>setText</code> method, it does not update on the screen. The initial call to <code>setText</code> is in in a method called <code>recordClap()</code>.</p> <pre><code> /**set text view*/ textView.setText("listening..."); </code></pre> <p>This text is not updated to the screen.</p> <p>Lastly, I set the text to display "Success!" once certain conditions have been met. </p> <pre><code> textView.setText("Success!"); </code></pre> <p>For some reason this is the only call to 'setText' that works.</p> <p>So, why is the TextView not updating the new text properly? Is there something I have left out? </p> <p>Full code below:</p> <pre><code>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 &gt;= 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(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload