Back to project page ScienceQuiz.
The source code is released under:
GNU General Public License
If you think the Android project ScienceQuiz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.finger.sciencequiz; /* ww w . j a v a 2 s . c om*/ import com.finger.sciencequiz.R; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.util.LruCache; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which * will keep every loaded fragment in memory. If this becomes too memory * intensive, it may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ PagerAdapter mHomePagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; private LruCache<String, Bitmap> mMemoryCache; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mHomePagerAdapter = new HomePagerAdapter (getSupportFragmentManager (), this); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById (R.id.pager); mViewPager.setAdapter (mHomePagerAdapter); mViewPager.setCurrentItem (1); // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime ().maxMemory () / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap> (cacheSize) { @Override protected int sizeOf (String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getRowBytes () * bitmap.getHeight () / 1024; } }; } @Override public boolean onCreateOptionsMenu (Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater ().inflate (R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected (MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId (); switch (id) { case R.id.Action_Refresh: refresh (null); return true; case R.id.Action_NewGame: viewNewGame (null); return true; case R.id.Action_History: viewHistory (null); return true; case R.id.Action_Settings: viewSettings (null); return true; case R.id.Action_About: viewAbout (null); return true; } return super.onOptionsItemSelected (item); } public void addBitmapToMemoryCache (String key, Bitmap bitmap) { synchronized (mMemoryCache) { if (getBitmapFromMemCache (key) == null) { mMemoryCache.put (key, bitmap); } } } public Bitmap getBitmapFromMemCache (String key) { return mMemoryCache.get (key); } public void viewRunningGames (View view) { mViewPager.setCurrentItem (0); } public void viewFriends (View view) { mViewPager.setCurrentItem (2); } public void viewProfile (View view) { mViewPager.setCurrentItem (3); } public void viewNewGame (View view) { Intent intent = new Intent (this, NewGameActivity.class); startActivity (intent); } public void viewHistory (View view) { Intent intent = new Intent (this, HistoryActivity.class); startActivity (intent); } public void viewSettings (View view) { Intent intent = new Intent (this, SettingsActivity.class); startActivity (intent); } public void viewAbout (View view) { Intent intent = new Intent (this, AboutActivity.class); startActivity (intent); } public void refresh (View view) { Toast.makeText ( this, getResources ().getString (R.string.refreshing), Toast.LENGTH_LONG).show (); } }