Android Open Source - androll Roll Fragment






From Project

Back to project page androll.

License

The source code is released under:

Apache License

If you think the Android project androll 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 me.cybersensei.androll;
/*from w  ww.j a  va 2s.  c  o  m*/
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class RollFragment extends Fragment implements View.OnClickListener {
  public static final String TAG = Fragment.class.getCanonicalName();
  private RollListener listener;
  private Spinner dieSpinner;
  
  @Override
  public void onAttach(Activity activity) {
    listener = (RollListener) activity;
    super.onAttach(activity);
  } //onAttach
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    //Inflate view
    View view = inflater.inflate(R.layout.fragment_roll, container, false);
    
    //Create ArrayAdapter for the die sides
    ArrayAdapter<Integer> dieSides = new ArrayAdapter<Integer>(getActivity(), 
        android.R.layout.simple_spinner_item);
    
    //Set the drop down view resource
    dieSides.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    //Fill the ArrayAdapter
    for (int i = 0; i < Roll.DIE_SIDES.length; i++) {
      dieSides.add(Roll.DIE_SIDES[i]);
    } //for
    
    //Get the Spinner
    dieSpinner = (Spinner) view.findViewById(R.id.sp_die_sides);
    
    //Set the ArrayAdapter for the Spinner
    dieSpinner.setAdapter(dieSides);
    
    //SetOnClickListener
    view.findViewById(R.id.b_quick_roll).setOnClickListener(this);
    view.findViewById(R.id.b_roll).setOnClickListener(this);
    return view;
  } //onCreateView

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.b_quick_roll:
      quickRoll();
      break;
    case R.id.b_roll:
      roll();
      break;
    } //switch
  } //onClick
  
  private int getSides() {
    return (Integer)dieSpinner.getSelectedItem();
  } //getSides
  
  private void quickRoll() {
    try {
      //Get sides
      int sides = getSides();
      
      //Tell the listener about the new roll
      listener.newRoll(new Roll(sides));
    } //try
    catch (Exception e) {
      Log.e(TAG, e.getMessage());
      if (BuildConfig.DEBUG)
        e.printStackTrace();
    } //catch
  } //quickRoll
  
  private void roll() {
    try {
      //Start FragmentTransaction
      FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
      
      //Create args
      Bundle args = new Bundle();
      args.putInt(RollHistoryActivity.SIDES_POSITION_KEY, dieSpinner.getSelectedItemPosition());
      
      //Create Fragment
      RollerDialogFragment rdf = new RollerDialogFragment();
      
      //Supply Fragment with args
      rdf.setArguments(args);
      
      //Add Fragment
      ft.add(rdf, RollerDialogFragment.class.getSimpleName());
      
      //Commit transaction
      ft.commit();
    } //try
    catch (Exception e) {
      Log.e(TAG, e.getMessage());
      if (BuildConfig.DEBUG)
        e.printStackTrace();
    } //catch
  } //roll
} //class RollFragment





Java Source Code List

me.cybersensei.androll.RollFragment.java
me.cybersensei.androll.RollHistoryActivity.java
me.cybersensei.androll.RollListener.java
me.cybersensei.androll.Roll.java
me.cybersensei.androll.RollerDialogFragment.java