Monday, June 27, 2011

Brining the Custom Dialog under control

So, where we left off last time, I was puzzling over how to keep a Custom Dialog disappearing when the activity it belongs to goes away on a finish call. And, suddenly the old light bulb clicked on and I realized, I just have to have a listener to the custom dialog call the finish. It means it will need the customer to do some kind of gesture.

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:id="@+id/layout_root" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp">

android:layout_height="fill_parent" android:layout_marginRight="10dp" />
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:textColor="#FFF" />

No comments:

Post a Comment