Android Open Source - ManipalNow-Android Main Activity






From Project

Back to project page ManipalNow-Android.

License

The source code is released under:

Apache License

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

/*
 * Copyright 2013 The Android Open Source Project
 */*from   w  w w . j av  a2 s .co  m*/
 * 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.
 */

package com.example.nav;

import java.util.ArrayList;
import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

/**
 * This example illustrates a common usage of the DrawerLayout widget
 * in the Android support library.
 * <p/>
 * <p>When a navigation (left) drawer is present, the host activity should detect presses of
 * the action bar's Up affordance as a signal to open and close the navigation drawer. The
 * ActionBarDrawerToggle facilitates this behavior.
 * Items within the drawer should fall into one of two categories:</p>
 * <p/>
 * <ul>
 * <li><strong>View switches</strong>. A view switch follows the same basic policies as
 * list or tab navigation in that a view switch does not create navigation history.
 * This pattern should only be used at the root activity of a task, leaving some form
 * of Up navigation active for activities further down the navigation hierarchy.</li>
 * <li><strong>Selective Up</strong>. The drawer allows the user to choose an alternate
 * parent for Up navigation. This allows a user to jump across an app's navigation
 * hierarchy at will. The application should treat this as it treats Up navigation from
 * a different task, replacing the current task stack using TaskStackBuilder or similar.
 * This is the only form of navigation drawer that should be used outside of the root
 * activity of a task.</li>
 * </ul>
 * <p/>
 * <p>Right side drawers should be used for actions, not navigation. This follows the pattern
 * established by the Action Bar that navigation should be to the left and actions to the right.
 * An action should be an operation performed on the current contents of the window,
 * for example enabling or disabling a data overlay on top of the current content.</p>
 */
public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;
    static final int check=1111;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        //mDrawerList.setSelected(true);
        //mDrawerList.setSelection(0);
        mDrawerList.setSelector(R.drawable.abc_item_background_holo_dark);
        mDrawerList.setItemChecked(2, true);
        //ActionBar ab=getActionBar();
        //Resources r=getResources();
    //Drawable d=r.getDrawable(R.color.playpurple);
    //ab.setBackgroundDrawable(d);
    
    //ab.setIcon(R.drawable.restaurant);
        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        Level data[]=new Level[]
        {
         new Level("Restaurants",R.drawable.restaurantbw),
         new Level("Cafes",R.drawable.cafebw),
         new Level("Bars",R.drawable.barbw),
         new Level("Hospitals",R.drawable.hospitalbw),
         new Level("Airports",R.drawable.airportbw),
         new Level("Cab Services",R.drawable.cabbw),
         new Level("Hotels",R.drawable.hotelsbw)
        };
    
        LevelAdapter la=new LevelAdapter(this, R.layout.drawer_list_item_2, data);
        mDrawerList.setAdapter(la);
        mDrawerList.setChoiceMode(1);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        //ActionBar ab=getActionBar();
    //ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.navh,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
      //  menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         // The action bar home/up action should open or close the drawer.
         // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch(item.getItemId()) {
        case R.id.action_voice:
            // create intent to perform web search for this planet
        {   Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      i.putExtra(RecognizerIntent.EXTRA_PROMPT, "What would you like to do?");
      startActivityForResult(i, check);
      break;
        }
        case R.id.action_settings:
          {
            Intent i2=new Intent(MainActivity.this,Settings3.class);
          startActivity(i2);
          }
    default:
            return super.onOptionsItemSelected(item);
        }
    return false;
    }
    protected void onActivityResult(int request,int result,Intent data)
  {
    if(request==check&&result==RESULT_OK)
    {
      ArrayList<String>matches=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
      for(String match:matches)
      {
      String a=matches.get(0);
      if("i am hungry".equalsIgnoreCase(a)||"i'm hungry".equalsIgnoreCase(a))
      {
        Intent i=new Intent(MainActivity.this,Food.class);
        startActivity(i);
        overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);

        //Toast.makeText(getBaseContext(), "open list of restaurants", Toast.LENGTH_SHORT).show();
      }
      else if("i would like a cup of coffee".equalsIgnoreCase(a))
      {
        Intent i=new Intent(MainActivity.this,Coffee.class);
        startActivity(i);
        overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);

        //Toast.makeText(getBaseContext(), "open list of restaurants", Toast.LENGTH_SHORT).show();
      }
      
      
      else
      {
      
        //res=data.getStringExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      Toast.makeText(getBaseContext(), "something else", Toast.LENGTH_SHORT).show();
      }
    }
  }
  }


    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        //Fragment fragment = new PlanetFragment();
        //Bundle args = new Bundle();
        //args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        //fragment.setArguments(args);

        if(position==0)
        {
        Fragment frag1=new Restaurants();  
      FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, frag1).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        }
        else if(position==1)
        {
          Fragment frag2=new Cafes();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag2).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        else if(position==2)
        {
          Fragment frag3=new Bars();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag3).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        else if(position==3)
        {
          Fragment frag4=new Hospitals();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag4).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        else if(position==4)
        {
          Fragment frag5=new ShowCity5();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag5).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        else if(position==5)
        {
          Fragment frag6=new ShowCity6();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag6).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        else if(position==6)
        {
          Fragment frag7=new ShowCity7();
          FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, frag7).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mPlanetTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        }
        
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * Fragment that appears in the "content_frame", shows a planet
     */
    public static class PlanetFragment extends Fragment {
        public static final String ARG_PLANET_NUMBER = "planet_number";

        public PlanetFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt(ARG_PLANET_NUMBER);
            String planet = getResources().getStringArray(R.array.planets_array)[i];

            int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                            "drawable", getActivity().getPackageName());
            ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
            getActivity().setTitle(planet);
            return rootView;
        }
    }
}




Java Source Code List

com.example.nav.BarFragment.java
com.example.nav.BarInfo.java
com.example.nav.Bars.java
com.example.nav.CafeFragment.java
com.example.nav.CafeInfo.java
com.example.nav.Cafes.java
com.example.nav.Coffee.java
com.example.nav.Food.java
com.example.nav.HelpAct.java
com.example.nav.HelpFragment1.java
com.example.nav.HelpFragment2.java
com.example.nav.HelpFragment3.java
com.example.nav.HelpFragment4.java
com.example.nav.HelpFragment5.java
com.example.nav.HelpFragment6.java
com.example.nav.HelpFragment7.java
com.example.nav.HelpFragment8.java
com.example.nav.HospitalFragment.java
com.example.nav.HospitalInfo.java
com.example.nav.Hospitals.java
com.example.nav.ImageAdapter.java
com.example.nav.LevelAdapter.java
com.example.nav.Level.java
com.example.nav.MainActivity.java
com.example.nav.RestaurantFragment.java
com.example.nav.RestaurantInfo.java
com.example.nav.Restaurants.java
com.example.nav.Settings3.java
com.example.nav.ShowCity3.java
com.example.nav.ShowCity5.java
com.example.nav.ShowCity6.java
com.example.nav.ShowCity7.java
com.example.nav.util.SystemUiHiderBase.java
com.example.nav.util.SystemUiHiderHoneycomb.java
com.example.nav.util.SystemUiHider.java