Java tutorial
package com.oddsoft.buddhavoice2.view; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.mikepenz.aboutlibraries.Libs; import com.mikepenz.aboutlibraries.LibsBuilder; import com.mikepenz.google_material_typeface_library.GoogleMaterial; import com.mikepenz.iconics.IconicsDrawable; import com.oddsoft.buddhavoice2.BuddhaVoice; import com.oddsoft.buddhavoice2.R; import com.oddsoft.buddhavoice2.utils.Analytics; import com.oddsoft.buddhavoice2.utils.Constant; import com.oddsoft.buddhavoice2.view.base.BaseActivity; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends BaseActivity { private static final String TAG = "BuddhaVoice"; @Bind(R.id.navigation) NavigationView navigation; @Bind(R.id.drawerlayout) DrawerLayout drawerLayout; @Bind(R.id.tabs) TabLayout tabs; @Bind(R.id.pager) ViewPager pager; private AdView adView; private Analytics ga; private ActionBarDrawerToggle actionBarDrawerToggle; @Bind(R.id.toolbar) Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); ga = new Analytics(); ga.trackerPage(this); initActionBar(); initDrawer(); setSwipeBackEnable(false); pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); tabs.setupWithViewPager(pager); ADView(); } private void initActionBar() { setSupportActionBar(toolbar); } private void initDrawer() { navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { //Checking if the item is in checked state or not, if not make it in checked state if (menuItem.isChecked()) menuItem.setChecked(false); else menuItem.setChecked(true); //Closing drawer on item click drawerLayout.closeDrawers(); switch (menuItem.getItemId()) { case R.id.navSetting: startActivity(new Intent(MainActivity.this, Prefs.class)); break; case R.id.navAbout: new LibsBuilder() //provide a style (optional) (LIGHT, DARK, LIGHT_DARK_TOOLBAR) .withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR).withAboutIconShown(true) .withAboutVersionShown(true).withAboutAppName(getString(R.string.app_name)) .withActivityTitle(getString(R.string.about_title)) .withAboutDescription(getString(R.string.license)) //start the activity .start(MainActivity.this); break; } return false; } }); //change navigation drawer item icons navigation.getMenu().findItem(R.id.navSetting).setIcon( new IconicsDrawable(this).icon(GoogleMaterial.Icon.gmd_settings).color(Color.GRAY).sizeDp(24)); navigation.getMenu().findItem(R.id.navAbout) .setIcon(new IconicsDrawable(this).icon(GoogleMaterial.Icon.gmd_info).color(Color.GRAY).sizeDp(24)); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) { @Override public void onDrawerClosed(View drawerView) { // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank super.onDrawerClosed(drawerView); } @Override public void onDrawerOpened(View drawerView) { // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank super.onDrawerOpened(drawerView); } }; //Setting the actionbarToggle to drawer layout drawerLayout.setDrawerListener(actionBarDrawerToggle); //calling sync state is necessay or else your hamburger icon wont show up actionBarDrawerToggle.syncState(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } /*get activity intent back from VoiceListener*/ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case RESULT_OK: //blank break; default: break; } } @Override protected void onStart() { super.onStart(); getPrefs(); } @Override protected void onPause() { super.onPause(); if (adView != null) adView.pause(); getPrefs(); } @Override public void onResume() { super.onResume(); if (adView != null) adView.resume(); } @Override protected void onDestroy() { if (adView != null) adView.destroy(); super.onDestroy(); } public class ViewPagerAdapter extends FragmentPagerAdapter { private final String[] TITLES = { getString(R.string.tab1), getString(R.string.tab2) }; public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } @Override public int getCount() { return TITLES.length; } @Override public Fragment getItem(int position) { return TabFragment.newInstance(position); } } private void ADView() { LinearLayout adBannerLayout = (LinearLayout) findViewById(R.id.footerLayout); adView = new AdView(this); adView.setAdUnitId(Constant.ADMob_BuddhaVoiceMain); adView.setAdSize(AdSize.SMART_BANNER); adBannerLayout.addView(adView); AdRequest adRequest; if (BuddhaVoice.APPDEBUG) { //Test Mode adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(Constant.ADMob_TestDeviceID).build(); } else { adRequest = new AdRequest.Builder().build(); } adView.loadAd(adRequest); } }