Advertisement
amosref

MainActivity.class

Sep 13th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. package com.example.ref_apps.winnerplayer;
  2.  
  3. import android.content.Context;
  4. import android.content.res.Configuration;
  5. import android.content.res.Resources;
  6. import android.content.res.TypedArray;
  7. import android.os.Bundle;
  8. import android.support.v4.app.Fragment;
  9. import android.support.v4.app.FragmentManager;
  10. import android.support.v4.widget.DrawerLayout;
  11. //import android.support.v7.app.ActionBarActivity;
  12. import android.support.v7.app.ActionBarDrawerToggle;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.AdapterView;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ListView;
  20. import android.widget.Toast;
  21.  
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     private ListView mDrawerList;
  26.     private DrawerLayout mDrawerLayout;
  27.     private ArrayAdapter<String> mAdapter;
  28.     private ActionBarDrawerToggle mDrawerToggle;
  29.     private String mActivityTitle;
  30.     Context ctx;
  31.     Resources res;
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_main);
  37.  
  38.         ctx = getApplicationContext();
  39.         res = ctx.getResources();
  40.  
  41.         mDrawerList = (ListView)findViewById(R.id.navList);
  42.         mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
  43.         mActivityTitle = getTitle().toString();
  44.  
  45.         addDrawerItems();
  46.         setupDrawer();
  47.  
  48.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  49.         getSupportActionBar().setHomeButtonEnabled(true);
  50.     }
  51.  
  52.     private void addDrawerItems() {
  53.  
  54.         String[] options = res.getStringArray(R.array.option_names);
  55.         TypedArray icons = res.obtainTypedArray(R.array.option_icons);
  56.  
  57.         mAdapter = (new com.example.ref_apps.winnerplayer.ImageAndTextAdapter(ctx, R.layout.list_layout, options, icons));
  58.         mDrawerList.setAdapter(mAdapter);
  59.  
  60.         mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  61.             @Override
  62.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  63.                 //Toast.makeText(MainActivity.this, "Time for an upgrade! " + position, Toast.LENGTH_SHORT).show();
  64.                 selectDrawerItem(position);
  65.             }
  66.         });
  67.     }
  68.  
  69.     private void setupDrawer() {
  70.         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
  71.  
  72.             /** Called when a drawer has settled in a completely open state. */
  73.             public void onDrawerOpened(View drawerView) {
  74.                 super.onDrawerOpened(drawerView);
  75.                 getSupportActionBar().setTitle("Make your choice");
  76.  
  77.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
  78.             }
  79.  
  80.             /** Called when a drawer has settled in a completely closed state. */
  81.             public void onDrawerClosed(View view) {
  82.                 super.onDrawerClosed(view);
  83.                 getSupportActionBar().setTitle(mActivityTitle);
  84.                 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
  85.             }
  86.         };
  87.  
  88.         mDrawerToggle.setDrawerIndicatorEnabled(true);
  89.         mDrawerLayout.setDrawerListener(mDrawerToggle);
  90.     }
  91.  
  92.     public void selectDrawerItem(int option) {
  93.         // Create a new fragment and specify the planet to show based on
  94.         // position
  95.         Fragment fragment = null;
  96.  
  97.         Class fragmentClass;
  98.         //TODO when more fragments are invented - add cases!! 10/9/15
  99.         switch(option) {
  100.             case 0:
  101.                 fragmentClass = FragmentVideo.class;
  102.                 break;
  103.             case 1:
  104.                 fragmentClass = FragmentAudio.class;
  105.                 break;
  106.             default:
  107.                 fragmentClass = FragmentAudio.class;
  108.         }
  109.  
  110.         try {
  111.             fragment = (Fragment) fragmentClass.newInstance();
  112.         } catch (Exception e) {
  113.             e.printStackTrace();
  114.         }
  115.  
  116.         // Insert the fragment by replacing any existing fragment
  117.         FragmentManager fragmentManager = getSupportFragmentManager();
  118.         fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
  119.  
  120.         // Highlight the selected item, update the title, and close the drawer
  121.         mDrawerLayout.closeDrawers();
  122.     }
  123.  
  124.     @Override
  125.     protected void onPostCreate(Bundle savedInstanceState) {
  126.         super.onPostCreate(savedInstanceState);
  127.         // Sync the toggle state after onRestoreInstanceState has occurred.
  128.         mDrawerToggle.syncState();
  129.     }
  130.  
  131.     @Override
  132.     public void onConfigurationChanged(Configuration newConfig) {
  133.         super.onConfigurationChanged(newConfig);
  134.         mDrawerToggle.onConfigurationChanged(newConfig);
  135.     }
  136.  
  137.     @Override
  138.     public boolean onCreateOptionsMenu(Menu menu) {
  139.         // Inflate the menu; this adds items to the action bar if it is present.
  140.         getMenuInflater().inflate(R.menu.menu_main, menu);
  141.         return true;
  142.     }
  143.  
  144.     @Override
  145.     public boolean onOptionsItemSelected(MenuItem item) {
  146.         // Handle action bar item clicks here. The action bar will
  147.         // automatically handle clicks on the Home/Up button, so long
  148.         // as you specify a parent activity in AndroidManifest.xml.
  149.         int id = item.getItemId();
  150.  
  151.         //noinspection SimplifiableIfStatement
  152.         if (id == R.id.action_settings) {
  153.             return true;
  154.         }
  155.  
  156.         // Activate the navigation drawer toggle
  157.         if (mDrawerToggle.onOptionsItemSelected(item)) {
  158.             return true;
  159.         }
  160.  
  161.         return super.onOptionsItemSelected(item);
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement