Java tutorial
/* * Copyright 2013 The Android Open Source Project * * 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.irontec.mintzatu; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListView; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; import com.bugsense.trace.BugSenseHandler; import com.irontec.adapters.MenuListAdapter; import com.irontec.fragments.EsploratuFragment; import com.irontec.fragments.EzarpenakFragment; import com.irontec.fragments.LagunakFragment; import com.irontec.fragments.LekuakFragment; import com.irontec.fragments.ProfilaFragment; public class MainActivity extends SherlockFragmentActivity { // Declare Variable DrawerLayout mDrawerLayout; ListView mDrawerList; ActionBarDrawerToggle mDrawerToggle; MenuListAdapter mMenuAdapter; String[] title; String[] subtitle; int[] icon; // Fragment lekuakFragment = new LekuakFragment(); // Fragment lagunakFragment = new LagunakFragment(); // Fragment esploratuFragment = new EsploratuFragment(); // Fragment profilaFragment = new ProfilaFragment(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); BugSenseHandler.initAndStartSession(getApplicationContext(), "6e0ba9cd"); setContentView(R.layout.activity_main); title = new String[] { "Lekuak", "Lagunak", "Esploratu", "Profila", "Ezarpenak" }; // subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2", // "Subtitle Fragment 3" }; icon = new int[] { R.drawable.lekuak, R.drawable.lagunak, R.drawable.esploratu, R.drawable.profila, R.drawable.ezarpenak }; mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mMenuAdapter = new MenuListAdapter(this, title, icon); mDrawerList.setAdapter(mMenuAdapter); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { selectItem(0); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { if (mDrawerLayout.isDrawerOpen(mDrawerList)) { mDrawerLayout.closeDrawer(mDrawerList); } else { mDrawerLayout.openDrawer(mDrawerList); } } return super.onOptionsItemSelected(item); } 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) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); switch (position) { case 0: ft.replace(R.id.content_frame, new LekuakFragment()); break; case 1: ft.replace(R.id.content_frame, new LagunakFragment()); break; case 2: ft.replace(R.id.content_frame, new EsploratuFragment()); break; case 3: ft.replace(R.id.content_frame, new ProfilaFragment()); break; case 4: ft.replace(R.id.content_frame, new EzarpenakFragment()); break; } ft.commit(); mDrawerList.setItemChecked(position, true); mDrawerLayout.closeDrawer(mDrawerList); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } }