Skip to content

Instantly share code, notes, and snippets.

public class LoginDialogFragment extends DialogFragment implements
OnEditorActionListener, TextWatcher, OnShowListener {
private TextView password;
private TextView username;
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="textPassword" />
@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
<?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"
/**
* Interface for Activities to implement to receive the result (the
* username and password in this case)
*
*/
public interface LoginDialogListener {
void onFinishLoginDialog(String username, String password);
}
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 void performLogout() {
Toast.makeText(this,
"Received log out request!",
Toast.LENGTH_SHORT
).show();
}
@Override
@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 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
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);