Java tutorial
/* * Copyright 2012 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.manoj.macawplayer; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.FragmentTransaction; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.LinearLayout; import android.widget.TextView; import com.manoj.fragments.AlbumFragment; import com.manoj.fragments.ArtistFragment; import com.manoj.fragments.ImageGridFragment; import com.manoj.fragments.PlaylistFragment; import com.manoj.fragments.SongCustomFragment; import com.manoj.fragments.TestFragment; import com.manoj.helper.Utilities; @SuppressLint("NewApi") public class SwipeViewActivity extends FragmentActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the * three primary sections of the app. 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}. */ AppSectionsPagerAdapter mAppSectionsPagerAdapter; private Utilities utilities; /** * The {@link ViewPager} that will display the three primary sections of the app, one at a * time. */ ViewPager mViewPager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.drawable.theme_skyblue); requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.swipe_view); int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); TextView actionBarTextView = (TextView) findViewById(actionBarTitleId); if (actionBarTextView != null) { Log.i("actionBarTextView :", "not null"); actionBarTextView.setTextColor(Color.GREEN); } else Log.i("actionBarTextView :", "null"); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); // Set up the action bar. final ActionBar actionBar = getActionBar(); // Specify that the Home/Up button should not be enabled, since there is no hierarchical // parent. actionBar.setHomeButtonEnabled(false); // Specify that we will be displaying tabs in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //to remove the title and icon above the action bar getActionBar().setDisplayShowTitleEnabled(false); getActionBar().setDisplayShowHomeEnabled(false); // Set up the ViewPager, attaching the adapter and setting up a listener for when the // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); LinearLayout homeScreen = (LinearLayout) findViewById(R.id.swipeviewl); utilities = new Utilities(); utilities.colorSeter(homeScreen, getApplicationContext()); mViewPager.setAdapter(mAppSectionsPagerAdapter); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between different app sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by the adapter. // Also specify this Activity object, which implements the TabListener interface, as the // listener for when this tab is selected. actionBar.addTab( actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this)); /*Tab tab = actionBar.getTabAt(i); View v = tab.setgetCustomView(); v.setBackgroundResource(R.drawable.theme_skyblue);*/ } } @SuppressLint("NewApi") @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { public AppSectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new AlbumFragment(); case 1: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new ArtistFragment(); case 2: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return new SongCustomFragment(); case 3: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. //return new PlaylistFragment(); return new PlaylistFragment(); case 4: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. //return new PlaylistFragment(); return new ImageGridFragment(); default: return new TestFragment(); } } @Override public int getCount() { return 6; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return "Albums"; case 1: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return "Artists"; case 2: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return "Songs"; case 3: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return "Playlist"; case 4: // The first section of the app is the most interesting -- it offers // a launchpad into the other demonstrations in this example application. return "Layout"; default: return "Test"; } } } }