Java tutorial
/* * Copyright (C) 2014 Pedro Vicente Gmez Snchez. * * 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.romelapj.appvercine.activity; import android.app.Activity; import android.content.Intent; 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 com.astuetz.PagerSlidingTabStrip; import com.readystatesoftware.systembartint.SystemBarTintManager; import com.romelapj.appvercine.R; import com.romelapj.appvercine.fragment.CardFragment; import com.romelapj.appvercine.fragment.ContactoFragment; import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnClick; public class MainActivity extends ActionBarActivity { @InjectView(R.id.toolbar) Toolbar toolbar; @InjectView(R.id.tabs) PagerSlidingTabStrip tabs; @InjectView(R.id.pager) ViewPager pager; private PaginaAdapter adaptador; private Drawable oldBackground = null; private SystemBarTintManager mTintManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); setSupportActionBar(toolbar); mTintManager = new SystemBarTintManager(this); mTintManager.setStatusBarTintEnabled(true); adaptador = new PaginaAdapter(getSupportFragmentManager()); pager.setAdapter(adaptador); tabs.setViewPager(pager); final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()); pager.setPageMargin(pageMargin); pager.setCurrentItem(1); changeColor(getResources().getColor(R.color.azul)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_principal, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_contact: ContactoFragment.newInstance().show(getSupportFragmentManager(), "ContactFragment"); return true; } return super.onOptionsItemSelected(item); } private void changeColor(int newColor) { tabs.setBackgroundColor(newColor); mTintManager.setTintColor(newColor); 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); } } public class PaginaAdapter extends FragmentPagerAdapter { private final String[] TITULOS = { "Cartelera", "Proximamente" }; public PaginaAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int pos) { return TITULOS[pos]; } @Override public int getCount() { return TITULOS.length; } @Override public Fragment getItem(int pos) { return CardFragment.newInstance(pos); } } }