Skip to content

Instantly share code, notes, and snippets.

/**
* 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;
/**
* 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) {
@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
password.setOnEditorActionListener(this);
public class LoginDialogFragment extends DialogFragment implements
OnEditorActionListener, TextWatcher, OnShowListener {
Button logout = (Button) findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogoutDialogFragment logoutFragment = LogoutDialogFragment
.newInstance();
logoutFragment.show(getSupportFragmentManager(), "logout");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the username from the arguments, if one isn't passed
// we use a blank string so it still looks OK
String username = "";
if (getArguments().containsKey(USERNAME))
username = ", " + getArguments().getString(USERNAME);
// "Are you sure you want to log out, <username>?"
String message
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
listener.performLogout();
break;
case DialogInterface.BUTTON_NEGATIVE:
listener.cancelLogout();
break;
@Override
public void performLogout() {
Toast.makeText(this,
"Received log out request!",
Toast.LENGTH_SHORT
).show();
}
@Override
private static final String USERNAME = "Username";
...
public static LogoutDialogFragment newInstance(String username) {
LogoutDialogFragment fragment = new LogoutDialogFragment();
// Add the username to the bundle of arguments for the fragment
Bundle args = new Bundle();
args.putString(USERNAME, username);
fragment.setArguments(args);