Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically change the text in an AlertDialog while it is showing?
    primarykey
    data
    text
    <p>I have an AlertDialog that is displayed, and want the text to have a countdown. While trying to implement it in the following way, the dialog displays exactly as needed, but I am trying to figure out how to make one of the TextViews change to the timer while the dialog is displayed. When I try to implement it in this way, it crashes, as you can see in the log info.</p> <pre><code>public void startOverlay() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(false); // Get the layout inflater LayoutInflater inflater = getLayoutInflater(); View boxLayout = inflater.inflate(R.layout.overlay, null); builder.setView(boxLayout); Typeface flatui = Typeface.createFromAsset(getAssets(), "fonts/Flat-UI-Font.ttf"); TextView t1 = (TextView)boxLayout.findViewById(R.id.dialogtext1); final TextView t2 = (TextView)boxLayout.findViewById(R.id.dialogtext2); t1.setTypeface(flatui); t2.setTypeface(flatui); final AlertDialog receivedBox = builder.create(); receivedBox.show(); CountDownTimer timer = new CountDownTimer(6000, 1000) { public void onTick(long millisUntilFinished) { final int seconds = (int) (millisUntilFinished / 1000); log("Tick: " + seconds); if (seconds == 0) receivedBox.dismiss(); Handler mHandler = new Handler(); mHandler.post(new Runnable() { @Override public void run() { // executed on the UI thread t2.setText(seconds); } }); } public void onFinish() { receivedBox.dismiss(); log("done!"); beginFirst(); } }.start(); } </code></pre> <p>Log info:</p> <pre><code>06-24 12:01:18.977: E/AndroidRuntime(11682): FATAL EXCEPTION: main 06-24 12:01:18.977: E/AndroidRuntime(11682): android.content.res.Resources$NotFoundException: String resource ID #0x5 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.content.res.Resources.getText(Resources.java:242) 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.widget.TextView.setText(TextView.java:3773) 06-24 12:01:18.977: E/AndroidRuntime(11682): at com.workout.MainActivity$1$1.run(MainActivity.java:163) 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.handleCallback(Handler.java:615) 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.dispatchMessage(Handler.java:92) 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Looper.loop(Looper.java:137) 06-24 12:01:18.977: E/AndroidRuntime(11682): at android.app.ActivityThread.main(ActivityThread.java:4918) 06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invokeNative(Native Method) 06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invoke(Method.java:511) 06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 06-24 12:01:18.977: E/AndroidRuntime(11682): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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