0

I'm trying to create a ViewDrawer with an support-lib actionbaractivity. My layout looks like

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <include
        android:id="@+id/drawer_view"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        layout="@layout/include_drawer" />

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

And the activity looks something like this:

public abstract class BaseActivity extends BaseSimpleActivity {
    public static final String FRAGMENT_TAG = "content";
    protected DrawerLayout mDrawerLayout;
    protected ActionBarDrawerToggle mDrawerToggle;
    protected ListView mDrawerList;
    protected ViewGroup mDrawerView;
    protected View mDrawerBtnSettings;
    private Handler mHandler;

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



    protected void initViews(String title, Fragment fragment) {
        setContentView(R.layout.activity_base_secondary);
        getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment, FRAGMENT_TAG).commit();
        initDrawer();
        initActionBar();
    }

    protected void setTitle(String title) {
        super.setTitle(title);
        getSupportActionBar().setTitle(title);
    }

    private void initActionBar() {
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP);
        //actionBar.setIcon(R.drawable.ic_launcher);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(R.layout.action_bar);

        TextView title = (TextView) actionBar.getCustomView().findViewById(R.id.actionBarTitle);
        title.setText(getTitleResId());
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
                    mDrawerLayout.closeDrawers();
                } else {
                    mDrawerLayout.openDrawer(Gravity.START);
                }
            }
        });
        refreshActionBar();
    }

    public abstract String getTitleResId();

    private void initDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_container);
        mDrawerView = (ViewGroup) findViewById(R.id.drawer_view);

        mDrawerList = (ListView) findViewById(R.id.drawer_list);
        mDrawerList.setAdapter(new DrawerListAdapter(this));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = createDrawerToggle();
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        mDrawerBtnSettings = findViewById(R.id.drawer_settings);

        refreshDrawer();
    }

    private ActionBarDrawerToggle createDrawerToggle() {
        return new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        refreshActionBar();

        int size = menu.size();
        mMenuItemIds = new int[size];
        for (int i = 0; i < size; i++) {
            mMenuItemIds[i] = menu.getItem(i).getItemId();
        }

        return super.onPrepareOptionsMenu(menu);
    }

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

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

    protected boolean isDrawerOpen() {
        return mDrawerLayout.isDrawerOpen(mDrawerView);
    }

    /**
     * Update the title and invalidate the options menu
     */
    protected void refreshActionBar() {
        if (isDrawerOpen()) {
            setActionBarItemsAlpha(0);
        } else {
            getSupportActionBar().setTitle(getTitle());
        }
    }

    /**
     * Update the selected item.
     */
    protected void refreshDrawer() {
        Navigation id = Navigation.getId(this);
        if(id != null) {
            mDrawerList.setItemChecked(id.ordinal(), true);
        }
    }

    protected void onDrawerItemClick(int which) {
        // Override if subclass needs to listen for Drawer click events
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectDrawerItem(position);
        }
    }

    protected void selectDrawerItem(final int which) {
        Navigation id = Navigation.getId(this);
        if (id != null && which == id.ordinal()) {
            return;
        }

        onDrawerItemClick(which);
        mPendingLaunch = DrawerUtils.buildLaunchRunnable(this, which);

        mDrawerLayout.closeDrawer(mDrawerView);
    }
}

But as you can see on the screenshot below the icon for the drawer is rendered on some devices and on others its not rendered at all. When i check in android monitor the layout has a width of zero. Any clues on whats going on?

Failing drawer

Arneball
  • 359
  • 3
  • 10

1 Answers1

2

If you are using v21 of AppCompat, you need to use android.support.v7.app.ActionBarDrawerToggle rather than the deprecated v4 version you are using.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thank you. Next step would be to implement toolbar in order to be able to change the drawable for the drawer icon. Pity that one cannot do it without toolbar – Arneball Oct 23 '14 at 10:24
  • You sure? Styling the drawer icon if done from your theme certainly doesn't require the use of a Toolbar: http://stackoverflow.com/a/26440502/1676363 – ianhanniballake Oct 23 '14 at 13:40