com.e3h.usmcknowledge.activities.ranks.Warrant.java Source code

Java tutorial

Introduction

Here is the source code for com.e3h.usmcknowledge.activities.ranks.Warrant.java

Source

package com.e3h.usmcknowledge.activities.ranks;
/**
 * @author jameshovious <james@colibrifrost.com>
 *
 * Copyright 2012 Colibri Frost
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.devspark.sidenavigation.ISideNavigationCallback;
import com.devspark.sidenavigation.SideNavigationView;
import com.e3h.usmcknowledge.R;
import com.e3h.usmcknowledge.activities.MainActivity;
import com.e3h.usmcknowledge.activities.RankActivity;
import com.e3h.usmcknowledge.controllers.SideListViewController;

public class Warrant extends SherlockFragmentActivity
        implements ISideNavigationCallback, ActionBar.OnNavigationListener {

    private SideNavigationView sideNavigationView;
    ActionBar mActionBar;
    ViewPager mPager;
    ActionBar.Tab tab;
    private String[] mLocations;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_rank);

        sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
        sideNavigationView.setMenuItems(R.menu.side_navigation_menu);
        sideNavigationView.setMenuClickCallback(this);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mLocations = getResources().getStringArray(R.array.rankwarrant);

        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.rankwarrant,
                R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        // Activate Navigation Mode Tabs
        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);

        // Locate ViewPager in activity_main.xml
        mPager = (ViewPager) findViewById(R.id.pager);

        // Activate Fragment Manager
        FragmentManager fm = getSupportFragmentManager();

        // Capture ViewPager page swipes
        ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // Find the ViewPager Position
                mActionBar.setSelectedNavigationItem(position);
            }
        };

        mPager.setOnPageChangeListener(ViewPagerListener);
        // Locate the adapter class called ViewPagerAdapter.java
        ViewPagerAdapterMainMenu viewpageradapter = new ViewPagerAdapterMainMenu(fm);
        // Set the View Pager Adapter into ViewPager
        mPager.setAdapter(viewpageradapter);

        // Capture tab button clicks
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                // Pass the position on tab click to ViewPager
                mPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
        };

        String[] menuItems = { "USMC", "Navy", "Air Force", "Army", "Coast Guard" };

        for (int i = 0; i < menuItems.length; i++) {
            String item = menuItems[i];
            tab = mActionBar.newTab().setText(item).setTabListener(tabListener);
            mActionBar.addTab(tab);
        }

    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        switch (itemPosition) {
        case 0:
            break;
        case 1:
            startActivity(new Intent(Warrant.this, RankActivity.class));
            finish();
            break;
        case 2:
            startActivity(new Intent(Warrant.this, Officer.class));
            finish();
            break;
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            sideNavigationView.toggleMenu();
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    /**
     * We let the back button lead to the main menu.
     * This allows the activity stack to be present when navigating to other
     * apps from within our app.
     */
    @Override
    public void onBackPressed() {
        // hide menu if it shown
        if (sideNavigationView.isShown()) {
            sideNavigationView.hideMenu();
        } else {
            Intent mainIntent = new Intent(this, MainActivity.class);
            mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mainIntent);
            overridePendingTransition(R.anim.incoming, R.anim.outgoing);
            finish();
        }
    }

    /**
     * We pass the info to a controller containing our switch statement.
     * This allows us to update the menu via the switch in a single location
     * and saves us from writing repetitive code.
     */
    @Override
    public void onSideNavigationItemClick(int itemId) {
        SideListViewController mSideListViewController = new SideListViewController();
        mSideListViewController.onSideNavigationItemClick(this, itemId);
        overridePendingTransition(R.anim.incoming, R.anim.outgoing);
        finish();
    }

}