Skip to content

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/login_username_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
Log.i(TAG, "onCreateDialog");
LayoutInflater lf = LayoutInflater.from(getActivity());
/**
* Inflate our custom view for this dialog, it contains
* two TextViews and two EditTexts, one set for the username
* and a second set for the password
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="textPassword" />
public class LoginDialogFragment extends DialogFragment implements
OnEditorActionListener, TextWatcher, OnShowListener {
private TextView password;
private TextView username;
password.setOnEditorActionListener(this);
/**
* Perform the login if the user presses the Done key from the
* password field
*/
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
doLogin();
return true;
public class LoginDialogFragment extends DialogFragment implements
OnEditorActionListener, TextWatcher, OnShowListener {
/**
* We have to override setOnShowListener here (min API level 8) in
* order to validate the inputs before closing the dialog. Just
* overriding setPositiveButton closes the automatically when the
* button is pressed
*
* @return The onShowListener for the AlertDialog
*/
@Override
public void onShow(DialogInterface dialog) {
/**
* These TextWatchers are used to clear the error icons
* automatically once the user has remedied an error
*/
username.addTextChangedListener(this);
password.addTextChangedListener(this);
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {}
/**
* If the user receives an error message due to a blank field, this