0

I'm attempting to use a navigation drawer and an action bar in my application.

The top leftmost button in the action bar opens and closes the drawer, and does not have a title in the drawer closed state. But when the drawer is opened, the title reappears. The title is the project name. I'm looking to remove this title from every scenario. In the code below you can see I've tried to set it again inside drawer open and close

private NavigationDrawerFragment mNavigationDrawerFragment;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    setTitle("");
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setIcon(R.drawable.plus);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_closed)
    {
        /*
         * Called when a drawer has settled in a completely closed state
         */
        public void onDrawerClosed(View view)
        {
            Log.d("drawerToggle", "Drawer closed");
            super.onDrawerClosed(view);
            setTitle("");
            //getActionBar().setTitle(R.string.app_name);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
        }

        /*
         * Called when a drawer has settled in a completely open state
         */
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(false);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

Also the three horizontal lines that change when the drawer is open / closed, what's the best way to just completely remove these lines (but keep the main icon)? At the moment I've just set ic_drawer to a transparent image.

Also below is my code that changes the menu when the drawer is open / closed. It seems to work, but is this the best way to go about doing this?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        inflater.inflate(R.menu.main, menu);
        //restoreActionBar();
        System.out.println("inhere");
        //return true;
        //return super.onCreateOptionsMenu(menu);
    }
    else
        inflater.inflate(R.menu.close_left_drawer, menu);
    return super.onCreateOptionsMenu(menu);
}

Thanks.

Twentyonehundred
  • 2,199
  • 2
  • 17
  • 28
  • ucan see that anser link http://stackoverflow.com/questions/25618568/action-bar-custom-image-and-text-near-up-button/25619046?noredirect=1#comment40024050_25619046 – Mr. Borad Sep 03 '14 at 13:17
  • This does go some way towards a solution in that setting a custom display of a transparent xml layout removes the title, but it ends up with the navigation drawer appearing _underneath_ the custom ActionBar, instead of overlaying it properly. – Twentyonehundred Sep 03 '14 at 13:59

1 Answers1

0

setTitle will set the title and setIcon will set the icon to the ActionBar.

getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);

Reference :Adding Up Action and http://developer.android.com/guide/topics/ui/actionbar.html

  • setIcon sets the main icon, which is independent from the three horizontal lines that change when the navigation drawer is opened / closed. setTitle does alter the title, but when the navigation drawer is opened this is ignored, and the project title is reinstated. – Twentyonehundred Sep 03 '14 at 13:52