Created
October 31, 2011 18:23
-
-
Save pablisco/1328342 to your computer and use it in GitHub Desktop.
simple and pretty Prompt Dialog Builder [Android] [Java] [Dialogs] [UI]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.oos.kryten.dialogs; | |
import static com.google.common.base.Preconditions.checkNotNull; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.DialogInterface.OnClickListener; | |
import android.view.KeyEvent; | |
import android.view.View; | |
import android.view.View.OnKeyListener; | |
import android.widget.EditText; | |
public class PromptDialogBuilder extends AlertDialog.Builder { | |
private InternalListener listener; | |
private CharSequence positiveText; | |
private Integer positiveResourceId = android.R.string.ok; | |
private CharSequence negativeText; | |
private Integer negativeResourceId = android.R.string.cancel; | |
private static Integer padding; | |
private static final float PADDING = 10f; | |
public interface PromptListener { | |
public void onInputProvided(String input); | |
public void onCancel(); | |
public class Impl implements PromptListener { | |
@Override | |
public void onInputProvided(String input) { | |
} | |
@Override | |
public void onCancel() { | |
} | |
} | |
} | |
@Override | |
public AlertDialog create() { | |
if (positiveText != null) { | |
setPositiveButton(positiveText, listener); | |
} else { | |
setPositiveButton(positiveResourceId, listener); | |
} | |
if (negativeText != null) { | |
setNegativeButton(negativeText, listener); | |
} else { | |
setNegativeButton(negativeResourceId, listener); | |
} | |
AlertDialog dialog = super.create(); | |
listener.input = new EditText(getContext()); | |
int pad = padding; | |
listener.input.setOnKeyListener(listener); | |
dialog.setView(listener.input, pad, 0, pad, pad); | |
return dialog; | |
} | |
public PromptDialogBuilder(Activity activity) { | |
super(activity); | |
Display display = activity.getWindowManager().getDefaultDisplay(); | |
DisplayMetrics metrics = new DisplayMetrics(); | |
display.getMetrics(metrics); | |
padding = (int) (PADDING * metrics.density); | |
listener = new InternalListener(new PromptListener.Impl()); | |
} | |
public PromptDialogBuilder setPromptListener(PromptListener listener) { | |
this.listener = new InternalListener(listener); | |
return this; | |
} | |
public PromptDialogBuilder setPositiveButton(CharSequence text) { | |
positiveText = text; | |
return this; | |
} | |
public PromptDialogBuilder setPositiveButton(int textId) { | |
positiveResourceId = textId; | |
return this; | |
} | |
public PromptDialogBuilder setNegativeButton(CharSequence text) { | |
negativeText = text; | |
return this; | |
} | |
public PromptDialogBuilder setNegativeButton(int textId) { | |
negativeResourceId = textId; | |
return this; | |
} | |
private class InternalListener implements OnClickListener, OnKeyListener { | |
private final PromptListener listener; | |
private EditText input; | |
private InternalListener(PromptListener listener) { | |
super(); | |
if(listener == null) { | |
throw new NullPointerException("Listener can't be null"); | |
} | |
this.listener = listener; | |
} | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
switch (which) { | |
case AlertDialog.BUTTON_POSITIVE: | |
final CharSequence email = input.getText(); | |
listener.onEmailProvided(email.toString()); | |
case AlertDialog.BUTTON_NEGATIVE: | |
default: | |
dialog.dismiss(); | |
break; | |
} | |
} | |
@Override | |
public boolean onKey(View v, int keyCode, KeyEvent event) { | |
if (keyCode == KeyEvent.KEYCODE_ENTER) { | |
onClick(dialog, AlertDialog.BUTTON_POSITIVE); | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new PromptDialogBuilder(this) | |
.setPositiveButton(R.string.offer_signup) // sets the positive (ok) button text | |
// .setNegativeButton(R.string.cancel) // we can use it but the default should be sufficient | |
.setPromptListener(new PromptListener.Impl() { | |
@Override | |
public void onInputProvided(String input) { | |
// do whatever with the email | |
} | |
}) // | |
.setTitle(R.string.offer_signing) // This one now returns the AlertDialog.Builder and now we handle it as a normal Builder | |
.setMessage(R.string.offer_need_email) // | |
.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to implement with a custom layout ?