Skip to content

Instantly share code, notes, and snippets.

@longkai
Created February 10, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longkai/acd9e48b7ecf0e9eb164 to your computer and use it in GitHub Desktop.
Save longkai/acd9e48b7ecf0e9eb164 to your computer and use it in GitHub Desktop.
Base activity you might likely use when you set your theme to ``**NoActionBar**`` but with a content view.
/*
* The MIT License (MIT)
* Copyright (c) 2015 longkai
* The software shall be used for good, not evil.
*/
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
/**
* Base activity you might likely use when you set your theme to ``**NoActionBar**`` but with a content view.
*
* This activity simple provide you a toolbar, and override some common used method from the default activity.
*
* like ``setContentView()``, setTitle()``, etc. The root view is an vertical linear layout(you may have no interest).
*
* Be sure, if you want to use menu, you must use the toolbar style, since we don' t set support with the toolbar.
*
* @author longkai
*/
public class ToolbarActivity extends ActionBarActivity {
private LinearLayout mRootView;
private Toolbar mToolbar;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRootView = new LinearLayout(this);
mRootView.setOrientation(LinearLayout.VERTICAL);
mRootView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
));
mToolbar = new Toolbar(this);
// let' s give some color to cc
final Resources res = getResources();
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int colorPrimaryResId = typedValue.resourceId;
getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true);
int actionBarSizeResId = typedValue.resourceId;
// what if TypedValue has a recycle() method?
mToolbar.setBackgroundColor(res.getColor(colorPrimaryResId));
mToolbar.setMinimumHeight(res.getDimensionPixelSize(actionBarSizeResId));
mToolbar.setTitleTextAppearance(this, R.style.TextAppearance_AppCompat_Widget_ActionBar_Title_Inverse);
mRootView.addView(mToolbar, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
super.setContentView(mRootView);
}
@Override public void setContentView(View view) {
// 'Cause always have one(index at **1** of the root view) content view, 0 is the Toolbar
mRootView.addView(view, 1);
}
@Override public void setContentView(int layoutResID) {
getLayoutInflater().inflate(layoutResID, mRootView, true);
}
@Override public void setContentView(View view, ViewGroup.LayoutParams params) {
mRootView.addView(view, params.width, params.height);
}
@Override public void setTitle(CharSequence title) {
mToolbar.setTitle(title);
}
@Override public void setTitle(int titleId) {
setTitle(getText(titleId));
}
public Toolbar getToolbar() {
return mToolbar;
}
public LinearLayout getRootView() {
return mRootView;
}
// public void setDisplayHomeAsUpEnable() {
// mToolbar.setNavigationIcon(R.attr.homeAsUpIndicator);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment