Java tutorial
/* Copyright (C) 2013-2014 Christian Schneider christian.d.schneider@googlemail.com This file is part of Androsens 2. Androsens 2 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Androsens 2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Androsens 2. If not, see <http://www.gnu.org/licenses/>. */ package com.tritop.androsense2; import java.util.Locale; import com.tritop.androsense2.fragments.GpsFragment; import com.tritop.androsense2.fragments.GpsNmeaFragment; import com.tritop.androsense2.fragments.SensorsListFragment; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.NavUtils; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends FragmentActivity implements OnPageChangeListener { SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((AndrosensApp) this.getApplication()) .setSensorManager((SensorManager) getSystemService(Context.SENSOR_SERVICE)); ((AndrosensApp) getApplication()) .setSensorList(((AndrosensApp) getApplication()).getSensorManager().getSensorList(Sensor.TYPE_ALL)); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setOnPageChangeListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: Intent startIntent = new Intent(this, SettingsActivity.class); startActivity(startIntent); return true; default: return super.onOptionsItemSelected(item); } } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { private SensorsListFragment sensfragment; private GpsFragment gpsfragment; private GpsNmeaFragment gpsNmeafragment; public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: sensfragment = new SensorsListFragment(); return sensfragment; case 1: gpsfragment = new GpsFragment(); return gpsfragment; case 2: gpsNmeafragment = new GpsNmeaFragment(); return gpsNmeafragment; default: return null; } } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { } }