Back to project page MythTrack.
The source code is released under:
MIT License
If you think the Android project MythTrack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * CreateHeroDialog presents a dialog for the user to create a new hero. It has the user enter a * name for the hero and select the hero's class. *//from w ww. j av a 2s .c o m * @author Nolan Jurgens */ package nolanjurgens.mythtrack.app; // IMPORTS ///////////////////////////////////////////////////////////////////////////////////////// import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import nolanjurgens.mythtrack.R; import nolanjurgens.mythtrack.provider.MythTrackContract; //////////////////////////////////////////////////////////////////////////////////////////////////// // CLASS - CreateHeroDialog // //////////////////////////////////////////////////////////////////////////////////////////////////// public class CreateHeroDialog extends DialogFragment { // INTERFACES //////////////////////////////////////////////////////////////////////////////////// /** Interface for a listener for when a hero is created.*/ OnHeroCreatedListener callback; public interface OnHeroCreatedListener { public void onHeroCreated(String heroName, int heroClass); } // METHODS /////////////////////////////////////////////////////////////////////////////////////// /** * Called when the fragment is attached to the activity. * @param activity The host activity. */ @Override public void onAttach(Activity activity) { super.onAttach(activity); // Make sure the host activity implements the interface. try { callback = (OnHeroCreatedListener) activity; } catch(ClassCastException exception) { throw new ClassCastException(activity.toString() + " must implement onHeroCreated"); } } /** * Called when the dialog is to be created. * @param arguments Arguments for the dialog. * @return The created dialog. */ @Override public Dialog onCreateDialog(Bundle arguments) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Create the input fields from the layout. LayoutInflater inflater = getActivity().getLayoutInflater(); LinearLayout inputViews = (LinearLayout) inflater.inflate(R.layout.fragment_create_hero_dialog, null); // Get handles to the individual fields. final EditText heroName = (EditText) inputViews.findViewById(R.id.createherodialog_name); final Spinner heroClass = (Spinner) inputViews.findViewById(R.id.createherodialog_class); // Build spinner adapter with class names. ArrayAdapter<String> spinnerClasses = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item); spinnerClasses.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Base Classes. spinnerClasses.add(getString(R.string.acolyte)); spinnerClasses.add(getString(R.string.apprentice)); spinnerClasses.add(getString(R.string.archer)); spinnerClasses.add(getString(R.string.brigand)); spinnerClasses.add(getString(R.string.soldier)); // Expansion Classes. SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); if(preferences.getBoolean("pref_key_enable_druid", false)) { spinnerClasses.add(getString(R.string.druid)); } if(preferences.getBoolean("pref_key_enable_skald", false)) { spinnerClasses.add(getString(R.string.skald)); } if(preferences.getBoolean("pref_key_enable_spriggan", false)) { spinnerClasses.add(getString(R.string.spriggan)); } if(preferences.getBoolean("pref_key_enable_trickster", false)) { spinnerClasses.add(getString(R.string.trickster)); } // TODO - Sort Spinner/List. // Spinner to use adapter. heroClass.setAdapter(spinnerClasses); // Set the title. builder.setTitle(R.string.createherodialog_title); // Insert the input fields. builder.setView(inputViews); // Set up the "Create" button and its action. builder.setPositiveButton(R.string.createherodialog_positive, new DialogInterface.OnClickListener() { /** * Handle "Create" button clicks. * @param dialogInterface The dialog that was clicked. * @param clickedButton Which button was clicked. */ @Override public void onClick(DialogInterface dialogInterface, int clickedButton) { // Get the entered values. String heroNameValue = heroName.getText().toString(); String heroClassValue = heroClass.getSelectedItem().toString(); // Set the class ID from the class string. int heroClassID = 0; if(heroClassValue.equals(getText(R.string.acolyte))) { heroClassID = MythTrackContract.Heroes.ACOLYTE; } else if(heroClassValue.equals(getText(R.string.apprentice))) { heroClassID = MythTrackContract.Heroes.APPRENTICE; } else if(heroClassValue.equals(getText(R.string.archer))) { heroClassID = MythTrackContract.Heroes.ARCHER; } else if(heroClassValue.equals(getText(R.string.brigand))) { heroClassID = MythTrackContract.Heroes.BRIGAND; } else if(heroClassValue.equals(getText(R.string.druid))) { heroClassID = MythTrackContract.Heroes.DRUID; } else if(heroClassValue.equals(getText(R.string.skald))) { heroClassID = MythTrackContract.Heroes.SKALD; } else if(heroClassValue.equals(getText(R.string.soldier))) { heroClassID = MythTrackContract.Heroes.SOLDIER; } else if(heroClassValue.equals(getText(R.string.spriggan))) { heroClassID = MythTrackContract.Heroes.SPRIGGAN; } else if(heroClassValue.equals(getText(R.string.trickster))) { heroClassID = MythTrackContract.Heroes.TRICKSTER; } // Create the hero via the activity. callback.onHeroCreated(heroNameValue, heroClassID); } }); // Set up the "Cancel" button and its action. builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { /** * Handle "Cancel" button clicks. * @param dialogInterface The dialog that was clicked. * @param clickedButton Which button was clicked. */ @Override public void onClick(DialogInterface dialogInterface, int clickedButton) { // Nothing special. Just exit dialog. } }); // Create the dialog. AlertDialog dialog = builder.create(); // Disable the "Create" button until a valid name is entered. dialog.show(); final Button createButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); createButton.setEnabled(false); // Watch for and handle changes in the name input field. heroName.addTextChangedListener(new TextWatcher() { /** * Called when text is about to be replaced * @param charSequence String that is being changed. * @param start Position of where change is starting. * @param count Number of characters being removed. * @param after Number of characters being added. */ @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { } /** * Called when text has just been changed. * @param charSequence String that is being changed. * @param start Position of where change is starting. * @param before Number of characters that were removed. * @param count Number of characters that were added. */ @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { } /** * Called whenever the text field is changed. * @param editable The text field that was changed. */ @Override public void afterTextChanged(Editable editable) { String heroNameValue = heroName.getText().toString(); // If the name is blank, disable "Create" button and display error. if(heroNameValue.trim().equalsIgnoreCase("")) { createButton.setEnabled(false); heroName.setError(getString(R.string.createherodialog_blank_name_error)); } // If the name is not blank, enable the "Create" button. else { createButton.setEnabled(true); } } }); return dialog; } }