Android Tutorial Android Tutorial Part I

View the Project on GitHub FatFractal/hoodyoodoo

Create Login Dialog

First, we need to create a new layout file. Create a file called login.xml in your layout directory, and enter the following code:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/userNameTextView"
    android:text="User Name"
    android:textStyle="bold" />
    <EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/userNameEditText"
    android:inputType="text" />
    <TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/passwordTextView"
    android:text="Password"
    android:textStyle="bold" />
    <EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/passwordEditText"
    android:inputType="textPassword" />
    <TextView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/loginRegisterErrorTextView"
    android:textStyle="bold"
    android:gravity="center" />
</LinearLayout>

The resulting layout should look something like this:

Then add the following method to the Hoodyoodoo class. The method provides a login dialog that can be used from anywhere in the app:

public AlertDialog loginDialog(Context c, String message) {
    LayoutInflater factory = LayoutInflater.from(c);            
    final View textEntryView = factory.inflate(R.layout.login, null);
    final AlertDialog.Builder failAlert = new AlertDialog.Builder(c);
    failAlert.setTitle("Login/ Register Failed"); 
    failAlert.setNegativeButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
            // Cancelled
        }
    });
    AlertDialog.Builder alert = new AlertDialog.Builder(c); 
    alert.setTitle("Login/ Register"); 
    alert.setMessage(message); 
    alert.setView(textEntryView); 
    alert.setPositiveButton("Login", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
            try {
                final EditText usernameInput = (EditText) textEntryView.findViewById(R.id.userNameEditText);
                final EditText passwordInput = (EditText) textEntryView.findViewById(R.id.passwordEditText);
                ff.login(usernameInput.getText().toString(), passwordInput.getText().toString());
            }
            catch (FFException e) {
                e.printStackTrace();
            }
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled. 
        }
    }); 
    return alert.create();
}

That's it for the Hoodyoodoo class! Now we're ready to move on and create some of the UI and controllers for the app!

NEXT: Add a Tab Activity