Android Open Source - Android-Wizard-Framework Main Activity






From Project

Back to project page Android-Wizard-Framework.

License

The source code is released under:

MIT License

If you think the Android project Android-Wizard-Framework 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

package com.hps.wizard.sample.activities;
//  ww w .j  ava 2s  .c o m
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.hps.wizard.AbstractWizardActivity;
import com.hps.wizard.StateFragment.StateDefinition;
import com.hps.wizard.WizardActivity;
import com.hps.wizard.WizardDialog;
import com.hps.wizard.sample.R;
import com.hps.wizard.sample.states.Instructions;

/**
 * The main {@link Activity}.
 */
public class MainActivity extends Activity {

  private static final int WIZARD_REQUEST = 1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /**
     * Set the start button to launch the wizard.
     */
    Button start = (Button) findViewById(R.id.start);
    start.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        launchWizard();
      }
    });

  }

  /**
   * Launches the wizard. See the inline comments for more details.
   */
  private void launchWizard() {
    /**
     * Step one: load a bundle with the arguments that will be passed to the first state. In this case we want to pass in the name they entered.
     */
    Bundle args = new Bundle();

    EditText nameField = (EditText) findViewById(R.id.nameField);
    String name = nameField.getText().toString();
    args.putString(Instructions.NAME_ARG, name);

    /**
     * Step two: determine the step count. If this is unknown or you don't want to display the step counter, send WizardActivity.STEP_COUNT_UNKNOWN to be
     * explicit or nothing at all.
     * 
     * In this case we use the check box to determine if we should show it.
     */
    CheckBox stepCountKnownCheckbox = (CheckBox) findViewById(R.id.stepCountKnownCheckbox);
    int stepCount = AbstractWizardActivity.STEP_COUNT_UNKNOWN;
    if (stepCountKnownCheckbox.isChecked()) {
      stepCount = 7;
    }

    /**
     * Step three: determine if you'd like a placeholder on all the screens for the neutral button. In the case of this sample we send true if the user
     * checked the box.
     */
    CheckBox allowNeutralPlaceholderCheckbox = (CheckBox) findViewById(R.id.allowNeutralPlaceholderCheckbox);
    boolean allowNeutralPlaceholder = allowNeutralPlaceholderCheckbox.isChecked();

    /**
     * Step four: determine if you'd like a placeholder on all the screens for the previous button. In the case of this sample we send true if the user
     * checked the box.
     */
    CheckBox allowPreviousPlaceholderCheckbox = (CheckBox) findViewById(R.id.allowPreviousPlaceholderCheckbox);
    boolean allowPreviousPlaceholder = allowPreviousPlaceholderCheckbox.isChecked();

    /**
     * Step five: Build the definition for the state. Because the first state is Instructions, we use that as the class.
     */
    StateDefinition def = new StateDefinition(Instructions.class, args);

    /**
     * Step six: call WizardActivity.startWizard() or WizardDialog.startWizard based on the user's selection. This will launch the wizard and load the first
     * state. The last argument is the request code. It will be used when the wizard is finished. If you don't care to wait for a response in
     * onActivityResult you can send a 0.
     */
    CheckBox showAsDialogCheckbox = (CheckBox) findViewById(R.id.showAsDialogCheckbox);
    if (showAsDialogCheckbox.isChecked()) {
      WizardDialog.startWizard(this, def, stepCount, allowPreviousPlaceholder, allowNeutralPlaceholder, WIZARD_REQUEST);
    } else {
      WizardActivity.startWizard(this, def, stepCount, allowPreviousPlaceholder, allowNeutralPlaceholder, WIZARD_REQUEST);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    /**
     * No menu in this sample application.
     */
    return false;
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    /**
     * The wizard is done. Get the results and show them in an alert dialog. The result data is generated by the last state's getFinalResult() method.
     */
    if (requestCode == WIZARD_REQUEST) {
      if (resultCode == RESULT_CANCELED) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Results");
        builder.setMessage("The wizard told me you're a quitter!");
        builder.setPositiveButton("Ok", null);
        builder.create().show();
      } else if (resultCode == RESULT_OK) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Results");
        builder.setMessage("The wizard told me your score was " + data.getIntExtra("score", -1));
        builder.setPositiveButton("Ok", null);
        builder.create().show();
      }
    }
  }

}




Java Source Code List

com.hps.wizard.AbstractWizardActivity.java
com.hps.wizard.StateFragment.java
com.hps.wizard.TaskCallback.java
com.hps.wizard.TaskFragment.java
com.hps.wizard.ValidationAsyncTask.java
com.hps.wizard.WizardActivity.java
com.hps.wizard.WizardDialog.java
com.hps.wizard.sample.activities.MainActivity.java
com.hps.wizard.sample.states.AreYouSure.java
com.hps.wizard.sample.states.BackgroundValidation.java
com.hps.wizard.sample.states.Choice.java
com.hps.wizard.sample.states.Instructions.java
com.hps.wizard.sample.states.MuppetShow.java
com.hps.wizard.sample.states.Results.java
com.hps.wizard.sample.states.SesameStreet.java
com.hps.wizard.sample.states.Seuss.java