[ Click to return to the Blog Index ]

Stop dismissing a Dialog, Android.

A dark hack I had to implement…

2012 January 12

Have I ever told you I hate Java? I haven’t? Really? Ok. I do. And may that be the disclaimer for this post. I’ve been doing Android development for quite a while now and the jewel I came into this morning just had to be blogged.

When you want to show a single or multi-selection dialog, you use an AlertDialog.Builder right?. You call “setMultiChoiceItems”, then you call “setPositiveButton” and “setNegativeButton” (if it applies), and finally you just “show()” it. All good? Ok.


If you want to control what happens when the positive button is clicked, go back to your “setPositiveButton” call and just create a listener of type “DialogInterface.OnClickListener”, then override the “onClick” method. Still all good? Great.

Now let’s assume you want to validate some stuff in your dialog and if that stuff is not valid, then you have to prevent the dialog from dismissing, ok? Here’s where it gets messy. You can override the “onClick” of your positive button as much as you want, but the dialog will be dismissed no matter what you do in there…

Why is this happening? Well, I could try to give you a technical explanation but the truth is, I have no clue… I just resort to the classic: “Java sucks” and “Android sucks”...

So how can you solve this? Google and StackOverflow will tell you that you should create a custom dialog for this. Fuck it. A custom dialog? For that shit? Really?

NO. There’s another way. A dark one. One future developers will have a hard time understanding. But it works and here’s the trick.

Instead of using AlertDialog.Builder, use an AlertDialog object; still use the Builder to set the items and the two buttons, but just call the “create()” method in the end. You’ll still want to assign a listener to your positive button, but put nothing in it…



AlertDialog programBuilder = new AlertDialog.Builder(this)

.setTitle(“Title here” )

.setMultiChoiceItems(......)

.setPositiveButton(“Ok”,

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int whichButton) {

// nothing to see here, move along

// real validation happens below.

}

})

.setNegativeButton(“Cancel”, null)

.create();

You must be wondering “wtf?”. Well, here’s the magic:


final AlertDialog pb = programBuilder;
programBuilder.setOnShowListener(

new DialogInterface.OnShowListener() {

@Override

public void onShow(DialogInterface dialog) {

Button btnOK = pb.getButton(AlertDialog.BUTTON_POSITIVE);

btnOK.setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

pb.dismiss();

}

}

});

}

});
programBuilder.show();

YES. A listener that overrides the “onShow” event, with another listener to the positive button that overrides its “onClick” method. And there, deep inside the overrides and the listeners, is the piece of code that you need… the “dismiss()”.

At this point, you can then put your validation and decide if the dialog goes away or stays. Something that is actually quite simple to do in Xcode/iOS, if you ask me.

120 days ago, Lillian_Delaware said:

Is there a way use some sort of third party forum moderation to cut through this problem? Then you could go on in whatever way you are comfortable, your forums could coninue to buzz and anyone that’s up to no good would hit a brick wall (or get a call from their local authorities, as appropriate). It’s just a thought., zac efron naked, uakm, autumn reeser nude, tvmo, alyson hannigan hot, 82301, bar refaeli naked, vsp, manisha koirala hot, 8PP, elle macpherson naked, zeqb, sarah jessica parker sex scene, >:-), kareena kapoor ass, 718, candice michelle hot, 8-O,

120 days ago, Natalia_Montana said:

In relation to the Technorati rank it’s pulled down via an API (geez I think that’s what it’s called) as is Alexa rank & the script calculates the index – I just need to record the figures in a spreadsheet for upload., anna friel nude, 455368, jessica lowndes nude, 20421, kate winslet nude pics, kqddyu, vida guerra naked, 79377, shilpa shetty sex, :(((, jennifer aniston fake, 414, gwen stefani nude, oxq, olivia munn nip slip, 847,

120 days ago, Samantha_Louisiana said:

Just look at the difference just 20-30 rock solid Tea Party conservatives have made in the House already. They’ve turned “1/3 of the government” on its head and started a war between the establishment party and the Tea Party., eva mendes sexy, 816, aishwarya rai sex scene, >:O, suzi perry nude, %-)), kari byron ass, :-))), kelly lynch nude, 627298, lea michele nude, 934027,

120 days ago, Allison_South_Dakota said:

In relation to the Technorati rank it’s pulled down via an API (geez I think that’s what it’s called) as is Alexa rank & the script calculates the index – I just need to record the figures in a spreadsheet for upload., vanessa anne hudgens naked, zpt, kate winslet sex scene, 266307, jodie marsh nude, 031, ashley tisdale ass, dhdu, amanda bynes naked, 260955, natasha henstridge naked, 8775, kelly kelly hot, hboxfl,

120 days ago, Brianna_Arkansas said:

I was thinking the same thing about Alexa as there was quite a lot of movement. They might have readjusted their adjustment for “technology skew” – or what ever it was…, diane lane hot, 19991, anna paquin hot, 640869, john cena nude, %OO, christina hendricks sex, =(((, selena gomez naked, >:OO, keira knightley sexy, %-OO, ashley tisdale sex, zux,

120 days ago, Camila_Minnesota said:

I think that you’d probably do well in a consulting role where you are helping companies who want to improve in the area of creating passionate users (products and websites). Your knowledge of the field is shown well by your blogging, and maybe getting out in front of people will give you more of a reason to stay passionate about this subject area (if not the blog itself)., amanda tapping hot, jwd, elisha cuthbert sex, uhtcq, zhang ziyi nude, :-OO, emma watson porn, jsizc, olivia wilde topless, hzfw, vin diesel nude, 8862, leonardo dicaprio nude, %-OO, keira knightley porn, jdlkab,

commenting closed for this article

flickr

About me...

My real name is Allan and i'm 22 23 24 25 26 27 28 29 30 years old.

My mind, personality and the bunch of absurd posts that you'll read here have been influenced mostly by my career (Software Engineer), my exhobbie (Electronic Sports) and my past failure success failure success in love and relationships. Get ready for some a lot of sarcasm, irony, narcissism, egocentrism and a lot of other terms that end up with "ism"..., comedy and senseless stories about my life...

Search this blog...

Meet me on...

[ Facebook.com ] [ Last.FM ] [ Del.icio.us ] [ Twitter.com ] [ Hi5.com ]
[ Flickr.com ] [ LinkedIn.com ]