Android Open Source - MythTrack Stat Picker Dialog






From Project

Back to project page MythTrack.

License

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.

Java Source Code

/**
 * StatPickerDialog presents a number picker that can be used to modify the stats of a hero.
 */*from w  ww. j a  va  2 s .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.os.Bundle;
import android.widget.NumberPicker;

import nolanjurgens.mythtrack.R;

////////////////////////////////////////////////////////////////////////////////////////////////////
// CLASS - StatPickerDialog                                                                       //
////////////////////////////////////////////////////////////////////////////////////////////////////
public class StatPickerDialog extends DialogFragment
{
  // CONSTANTS /////////////////////////////////////////////////////////////////////////////////////

  public static final int STAT_GOLD = 1;
  public static final int STAT_SERENDIPITY = 2;
  public static final int STAT_THREAT = 3;
  public static final int STAT_VITALITY = 4;

  // INTERFACES ////////////////////////////////////////////////////////////////////////////////////

  /** Interface for listener for when a stat is set.*/
  OnStatSetListener callback;
  public interface OnStatSetListener
  {
    public void onStatSet(int stat, int value);
  }

  // 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 = (OnStatSetListener) activity;
    }
    catch(ClassCastException exception)
    {
      throw new ClassCastException(activity.toString()
        + " must implement onStatSet");
    }
  }


  /**
   * 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());
    final NumberPicker statPicker = new NumberPicker(getActivity());
    final int stat = getArguments().getInt("stat");

    // Set the range of the picker.
    switch(stat)
    {
      case STAT_GOLD:
      {

        statPicker.setMinValue(0);
        statPicker.setMaxValue(1000);
        break;
      }
      case STAT_SERENDIPITY:
      {

        statPicker.setMinValue(0);
        statPicker.setMaxValue(100);
        break;
      }
      case STAT_THREAT:
      {
        int minThreat = getArguments().getInt("min_threat");
        int maxThreat = getArguments().getInt("max_threat");
        statPicker.setMinValue(minThreat);
        statPicker.setMaxValue(maxThreat);
        break;
      }
      case STAT_VITALITY:
      {
        int maxVitality = getArguments().getInt("max_vitality");
        if(maxVitality > 18)
        {
          maxVitality = 18;
        }
        statPicker.setMinValue(0);
        statPicker.setMaxValue(maxVitality);
        break;
      }
    }

    // Disable wrapping.
    statPicker.setWrapSelectorWheel(false);

    // Set starting value.
    statPicker.setValue(getArguments().getInt("value"));

    // Set the title.
    switch(stat)
    {
      case STAT_GOLD:
      {
        builder.setTitle(R.string.statpickerdialog_gold_title);
        builder.setIcon(R.drawable.ic_stat_gold);
        break;
      }
      case STAT_SERENDIPITY:
      {
        builder.setTitle(R.string.statpickerdialog_serendipity_title);
        builder.setIcon(R.drawable.ic_stat_serendipity);
        break;
      }
      case STAT_THREAT:
      {
        builder.setTitle(R.string.statpickerdialog_threat_title);
        builder.setIcon(R.drawable.ic_stat_threat);
        break;
      }
      case STAT_VITALITY:
      {
        builder.setTitle(R.string.statpickerdialog_vitality_title);
        builder.setIcon(R.drawable.ic_stat_vitality);
        break;
      }
    }

    // Insert the input fields.
    builder.setView(statPicker);
    // Set up the "Set" button and its action.
    builder.setPositiveButton(R.string.statpickerdialog_positive,
      new DialogInterface.OnClickListener()
      {
        /**
         * Handle "Set" button clicks.
         * @param dialogInterface The dialog that was clicked.
         * @param clickedButton Which button was clicked.
         */
        @Override
        public void onClick(DialogInterface dialogInterface, int clickedButton)
        {
          // Delete the hero via the activity.
          callback.onStatSet(stat, statPicker.getValue());
        }
      });
    // 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.
    return builder.create();
  }

}




Java Source Code List

nolanjurgens.mythtrack.app.BuyItemDialog.java
nolanjurgens.mythtrack.app.CreateHeroDialog.java
nolanjurgens.mythtrack.app.DeleteHeroDialog.java
nolanjurgens.mythtrack.app.EquipmentInfoDialog.java
nolanjurgens.mythtrack.app.HeroDisplayFragment.java
nolanjurgens.mythtrack.app.HeroHelper.java
nolanjurgens.mythtrack.app.HeroList.java
nolanjurgens.mythtrack.app.HeroTrackerActivity.java
nolanjurgens.mythtrack.app.Hero.java
nolanjurgens.mythtrack.app.InventoryList.java
nolanjurgens.mythtrack.app.ItemHelper.java
nolanjurgens.mythtrack.app.ItemList.java
nolanjurgens.mythtrack.app.Item.java
nolanjurgens.mythtrack.app.MainActivity.java
nolanjurgens.mythtrack.app.RemoveItemDialog.java
nolanjurgens.mythtrack.app.SettingsActivity.java
nolanjurgens.mythtrack.app.SettingsFragment.java
nolanjurgens.mythtrack.app.StatInfoDialog.java
nolanjurgens.mythtrack.app.StatPickerDialog.java
nolanjurgens.mythtrack.app.TitleHelper.java
nolanjurgens.mythtrack.app.Title.java
nolanjurgens.mythtrack.provider.MythTrackBackup.java
nolanjurgens.mythtrack.provider.MythTrackContract.java
nolanjurgens.mythtrack.provider.MythTrackDatabase.java