0

My BaseDrawerActivity extends BaseActivity and I have a Toolbar in BaseActivity.

The toolbar shows with no issues in classes that extend BaseActivity, but ALL classes that extend BaseDrawerActivity just show a blank toolbar. The nav drawer works fine.

When I had one main activity class (BaseActivity + BaseDrawerActivity) the toolbar showed no problem and the nav drawer worked.

Why is my implementation here not showing the Toolbar? I debugged and getToolbar() is returning the toolbar for sure.

activity_base_drawer.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:foreground="?android:windowContentOverlay">

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

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

        </FrameLayout>

        <ListView
            android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            style="@style/NavDrawerListView" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@style/ToolbarOverlay"
    android:popupTheme="@style/ToolbarOverlay"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:minHeight="?attr/actionBarSize"
    android:elevation="10dp"/>

BaseDrawerActivity

public abstract class BaseDrawerActivity extends BaseActivity {

    private static final int LAYOUT_ID = R.layout.activity_base_drawer;

    private DrawerLayout mDrawerLayout;

    private ActionBarDrawerToggle mDrawerToggle;
    private NavDrawerAdapter mNavDrawerAdapter;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)){
            mDrawerLayout.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

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

        setupNavDrawer();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    private void setupNavDrawer() {
        final ListView navDrawerListView = (ListView) findViewById(R.id.navigation_drawer);

        View header = getLayoutInflater().inflate(R.layout.nav_drawer_header, null, false);
        mNavDrawerAvatarImageView = (ImageView) header.findViewById(R.id.avatar);
        mNavDrawerUsernameTextView = (CustomTextView) header.findViewById(R.id.username);
        navDrawerListView.addHeaderView(header);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                getToolbar(),
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                updateNavDrawerUserInfo();
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mNavDrawerAdapter = new NavDrawerAdapter(this);
        navDrawerListView.setAdapter(mNavDrawerAdapter);
        navDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
                onNavigationDrawerItemSelected(position);
            }
        });

        updateNavDrawerUserInfo();
    }

    private void updateNavDrawerUserInfo() {
        final DatabaseHelper db = DatabaseHelper.getInstance(this);
        if (db.doesUserExist(SharedPrefs.getUserId())) {
            final User currentUser = db.getUser(SharedPrefs.getUserId());

            if (currentUser != null) {
                if (currentUser.getAvatarType() != null) {
                    try {
                        mNavDrawerAvatarImageView.setImageDrawable(getResources().getDrawable(
                                ViewUtil.getAvatarHeadDrawableId(this,
                                        currentUser.getAvatarType())));
                    } catch (Resources.NotFoundException e) {
                        e.printStackTrace();
                    }
                }

                if (currentUser.getUsername() != null) {
                    mNavDrawerUsernameTextView.setText(currentUser.getUsername());
                }
            }
        }
    }
}

BaseActivity

private void setupToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("");
        getSupportActionBar().setSubtitle("");
        getSupportActionBar().setLogo(R.drawable.logo_toolbar);
    }

    mUpdatingSpinner = (ProgressBar) getLayoutInflater().inflate(
            R.layout.toolbar_updating_spinner, null, false);
    int dpInPixels = (int) getResources().getDimension(R.dimen.toolbar_updating_spinner);
    Toolbar.LayoutParams spinnerLp = new Toolbar.LayoutParams(dpInPixels, dpInPixels,
            Gravity.END);
    mToolbar.addView(mUpdatingSpinner, spinnerLp);
}
The Nomad
  • 7,155
  • 14
  • 65
  • 100

3 Answers3

0

you have to use setSupportActionBar(Toolbar toolbar).

This question should help.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Ashish Rawat
  • 5,541
  • 1
  • 20
  • 17
  • I do `setSupportActionBar` in my `BaseActivity`. Edited my post to include the code of my `Toolbar` setup method. Remember that the toolbar works in classes that extend `BaseActivity`. It's just the `BaseDrawerActivity`s. – The Nomad Feb 01 '16 at 04:17
  • Have you made sure that everything eventually inherits from ActionBarActivity – Ashish Rawat Feb 01 '16 at 04:19
  • Yes, my BaseActivity extends from `AppCompatActivity` – The Nomad Feb 01 '16 at 04:35
0

go to the AndroidManifest.xml file and replace

android:theme="@style/AppTheme"

with

android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

Timothy Schoonover
  • 3,195
  • 4
  • 29
  • 44
NEERAJ GUPTA
  • 125
  • 1
  • 11
  • My `AppTheme` style is: – The Nomad Feb 01 '16 at 04:43
0

I found the answer although I don't fully understand it.

I basically called setupToolbar() again in my BaseDrawerActivity right before I call setupNavDrawer() and for some reason this fixed it.

I think the reference to the Toolbar from BaseActivity wasn't retrieving correctly (even though I debugged and it had a reference to it). Another thing I tried is setSupportActionBar(mToolbar) right before setting up the nav drawer but that didn't work.

If anyone has any ideas as to why I have to setup the toolbar again in order for it to show I'd be glad to hear it!

The Nomad
  • 7,155
  • 14
  • 65
  • 100
  • Android developer core team, please come in on this one. Many are striving to get the Toolbar replacing the ActionBar, the problem often being that the ToolBar is empty. It is there, but empty. What to do? – carl Jul 21 '19 at 13:34