Let's google customdiaglog listener android.
It takes is to the Dialog documentation. Ok, this might be helpful.
setOnDismissListener(DialogInterface.OnDismissListener listener)
Set a listener to be invoked when the dialog is dismissed.
Now, the question is, what triggers the dismiss event?
It might be better just to show the message for a couple of seconds, just delay it with a timer or something like that. That way, there's no confusion and no buttons required. Let's go with that for now.
I think I have some timer code somewhere in the project already. For the progress bar, I think. Let's have a look.
Here it is, roughly:
totalMsecs = 10 * 1000; // 10 seconds
callInterval = 100;
/**
* CountDownTimer is for totalMsec and every onTick is callInterval
* milliseconds
*/
mCountDownTimer = new CountDownTimer(totalMsecs, callInterval) {
public void onTick(long millisUntilFinished) {
float fraction = millisUntilFinished / (float) totalMsecs;
// progress bar is based on scale of 1 to 100;
m_bar.setProgress((int) (fraction * 100));
}
public void onFinish() {
handleTimeOut();
}
}.start();
I'm questioning if this will work. If the countdown timer runs in a separate thread, then it probably no good for this purpose. Let's take a peek at the source:
http://grepcode.com/snapshot/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/
It has Handler, which has queues and uses synchronized calls. How can you have messaging without asynchronous processing? Anyway, you wouldn't be able to select an answer if it was blocking call. Forget it.
Ok, button it is. Let's make it very simple and standard for now. Google android button example.
It's right in the docs:
http://developer.android.com/reference/android/widget/Button.html
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
Let's add this. Wow, I didn't know you could specify a listener method in the xml. Let's try this
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"
android:onClick="myFinish" />
So, here's our new layout:
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp">
android:textColor="#FFF" />
android:layout_width="wrap_content" android:text="OK" android:onClick="myFinish" />
And we'll take the finish call into the myFinish() method
Their example:
public void selfDestruct(View view) {
// Kabloey
}
My code:
public void myFinish(View view) {
finish();
}
Well, that just totally blew the program up - it just disappeared when I hit the next level code. Let's check out the log.
Nothing, no problem. But, when I run it again, it works fine. I can see the dialog box, and I have to press the ok button to make it go way. But, then, it dies on a "myfinish" method not found.
Let's try a more traditional approach:
Button button = (Button) dialog.findViewById(R.id.next_level_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
QuestionActivity.this.finish();
}
});
Bingo. It works perfectly. Tomorrow we'll tackle improving the look and feel of this.
No comments:
Post a Comment