Java tutorial
/* * BaseActivity.java * Copyright (C) 2014 Amin Bandali <me@aminb.org> * * MATHTools is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MATHTools is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.aminb.mathtools.app.activity; import android.app.ActionBar; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentActivity; import org.aminb.mathtools.app.R; public abstract class BaseActivity extends FragmentActivity { protected ActionBar mActionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActionBar = getActionBar(); if (!this.getClass().isAssignableFrom(ContentActivity.class)) changeColor(R.color.mathtoolstheme_color); } private void changeColor(int newColor) { // change ActionBar color just if an ActionBar is available if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Drawable colorDrawable = new ColorDrawable(getResources().getColor(newColor)); Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom); LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable }); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { ld.setCallback(drawableCallback); } else { mActionBar.setBackgroundDrawable(ld); } // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler mActionBar.setDisplayShowTitleEnabled(false); mActionBar.setDisplayShowTitleEnabled(true); } } private final Handler handler = new Handler(); private Drawable.Callback drawableCallback = new Drawable.Callback() { @Override public void invalidateDrawable(Drawable who) { mActionBar.setBackgroundDrawable(who); } @Override public void scheduleDrawable(Drawable who, Runnable what, long when) { handler.postAtTime(what, when); } @Override public void unscheduleDrawable(Drawable who, Runnable what) { handler.removeCallbacks(what); } }; }