Java tutorial
/* * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com> * Modifications Copyright (C) 2015 eccyan <g00.eccyan@gmail.com> * * 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.eccyan.widget.sample; import com.eccyan.widget.SpinningTabStrip; import com.readystatesoftware.systembartint.SystemBarTintManager; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; 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.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import butterknife.ButterKnife; import butterknife.InjectView; public class MainActivity extends ActionBarActivity { @InjectView(R.id.toolbar) Toolbar toolbar; @InjectView(R.id.tabs) SpinningTabStrip tabs; @InjectView(R.id.pager) ViewPager pager; private MyPagerAdapter adapter; private Drawable oldBackground = null; private int currentColor; private SystemBarTintManager mTintManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); setSupportActionBar(toolbar); // create our manager instance after the content view is set mTintManager = new SystemBarTintManager(this); // enable status bar tint mTintManager.setStatusBarTintEnabled(true); adapter = new MyPagerAdapter(getSupportFragmentManager()); pager.setAdapter(adapter); tabs.setViewPager(pager); final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()); pager.setPageMargin(pageMargin); changeColor(getResources().getColor(R.color.green)); tabs.setOnTabReselectedListener(new SpinningTabStrip.OnTabReselectedListener() { @Override public void onTabReselected(int position) { Toast.makeText(MainActivity.this, "Tab reselected: " + position, Toast.LENGTH_SHORT).show(); } }); } @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_contact: QuickContactFragment.newInstance().show(getSupportFragmentManager(), "QuickContactFragment"); return true; } return super.onOptionsItemSelected(item); } private void changeColor(int newColor) { tabs.setBackgroundColor(newColor); mTintManager.setTintColor(newColor); // change ActionBar color just if an ActionBar is available Drawable colorDrawable = new ColorDrawable(newColor); Drawable bottomDrawable = new ColorDrawable(getResources().getColor(android.R.color.transparent)); LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable }); if (oldBackground == null) { getSupportActionBar().setBackgroundDrawable(ld); } else { TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld }); getSupportActionBar().setBackgroundDrawable(td); td.startTransition(200); } oldBackground = ld; currentColor = newColor; } public void onColorClicked(View v) { int color = Color.parseColor(v.getTag().toString()); changeColor(color); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("currentColor", currentColor); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); currentColor = savedInstanceState.getInt("currentColor"); changeColor(currentColor); } public class MyPagerAdapter extends FragmentPagerAdapter { private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid", "Top New Free", "Trending" }; public MyPagerAdapter(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 SuperAwesomeCardFragment.newInstance(position); } } }