First, we need to create a new layout file. Create a file called login.xml in your layout directory, and enter the following code:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <? 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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!