2

My activities extend from a generic base activity, where I declare and initialize public variables like context of type Context, activity of type Activity and mActionBar of type ActionBar.

So this avoids redundant initialization code in all my app's activities.

But with the advent of Toolbar, I am a little confused on how to do this. Toolbar is not like ActionBar and replaces it, but also extends it.

The ActionBar is a view object that is always available for retrieval, by ActionBar activity, and it sits above the views that are created. This is not declared in layout XML anywhere.

But Toolbar is declared only in layout XML, so I have to include it in each and every layout I create, or else I will not be able to access the Toolbar object.

I typically use setContentView(R.layout.mylayout) in the onCreate method of each individual activity. And then I have to initialize my Toolbar object after that using findViewById. Therefore I can't put this code in my BaseActivity's onCreate function because the setContentView wouldn't have been initialized yet.

Even if I created Toolbar programmatically with it's constructor, and attempted to add the view to the top of the hierarchy, I would still have to do this on a layout by layout, and activity by activity basis, because some layouts are RelativeLayout's as the root object, and others are different. So these will still have separate code considerations.

The reason I am curious about a good way for my activities to inherit Toolbar, is because it is a complete nightmare for Google to suddenly require Android 4.0-4.4 devices to use the v7 compatibility pack, replace the actionbar completely with the Toolbar object, use v4 compatibility pack fragments instead of native ones, all to use the latest design paradigms.

10
  • "hey check out v21 ! but your v17 device needs to use v4 objects to check it out"
    – CQM
    Commented Jan 29, 2015 at 3:49
  • You could set the content View, with the Toolbar, in the base Activity class, and inflate the subclasses' Views into a container ViewGroup in the base layout.
    – Mike M.
    Commented Jan 29, 2015 at 4:07
  • @MikeM. I feel like this will be a problem. I also use the NavigationDrawer's DrawerLayout, which comes in front of the ToolBar, and will need to be a root element. In other cases I also use other libraries that rely on being a root element
    – CQM
    Commented Jan 29, 2015 at 4:08
  • In that case, you could create a separate layout for the Toolbar and inflate and initialize it in the base's onCreate() method, but don't add it to the on-screen layout until after the subclass calls setContentView().
    – Mike M.
    Commented Jan 29, 2015 at 4:30
  • 1
    @kip2 I did figure out a solution, my base activity has a toolbar variable, but all my sub activities have to re-declare it, as it is now duplicated in all of my XML layouts. One activity does not override another activity's use of the Toolbar object
    – CQM
    Commented Apr 28, 2015 at 20:21

1 Answer 1

4

this is my implementation. Hope it helps someone.

public abstract class BaseActivity extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected boolean useToolbar() {
        return true;
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        configureToolbar(view);
        super.setContentView(view);
    }

    private void configureToolbar(View view) {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        if (toolbar != null) {
            if (useToolbar()) {
                setSupportActionBar(toolbar);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            } else {
                toolbar.setVisibility(View.GONE);
            }
        }
    }
}

From here on you just extend BaseActivity. If you don't want a toolbar you will have to override the useToolbar().

Don't forget to add in activity.xml at the top

<include layout="@layout/toolbar" />

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</merge>

Not the answer you're looking for? Browse other questions tagged or ask your own question.