Drawer/Back button in ActionBar not clickable when in back - android

I used the solution I found here to have the left button on the Action Bar work as Drawer Toggle when there's nothing in the backstack and back button when there is. Here's the implementation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
navigateTo(R.id.menu_drawer);
initDrawer();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.main_open_menu, R.string.main_close_menu)
{
public void onDrawerClosed(View view){
syncActionBarState();
}
public void onDrawerOpened(View view){
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
};
mOnBackStackChangedListener =
new android.support.v4.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
syncActionBarState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
}
private void syncActionBarState(){
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
mDrawerToggle.setDrawerIndicatorEnabled(backStackCount == 0);
if(backStackCount == 0){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
As Drawer Toggle the button works fine. When working as back button it doesn't even trigger OnOptionsItemClickListener. What did I miss?

You have implemented setDisplayHomeAsUpEnabled() method as
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
so you can make it work as described here which is a simple way to do so

Related

Navigation drawer hamburger icon not showing up with v7 ActionBarDrawerToggle

I've been trying to create a navigation drawer for an existing app. I've found a few tutorials for this, but most of them (including the official Android guide) appear to be for the v4 ActionBarDrawerToggle library, which has been deprecated. I'm trying to use the v7 library instead, but my ActionBarDrawerToggle doesn't seem to do what the documentation says it should do.
Edit: Modified my code as per the answer below. The hamburger icon now switches back and forth correctly, but when the user taps the hardware back button to go back to the main fragment of my app, the hamburger icon disappears entirely. Why does this happen?
private void addDrawerItems() {
String[] itemArray = {"About", "Nearby", "Settings", "Feedback",};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ContributionsActivity", "Item " + position + " selected");
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.title_activity_contributions);
setContentView(R.layout.activity_contributions);
//Set up navigation drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.drawer_list);
addDrawerItems();
setupDrawer();
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// enabling drawer toggle by clicking on the app icon.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case android.R.id.home:
if (mediaDetails.isVisible()) {
getSupportFragmentManager().popBackStack();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
You're missing sync state, add it and everything should be fine.

How to make navigation drawer toggle forcibly close all the time

I am using DrawerToggle with toolbar in my project. I have a requirement to make drawer toggle in active (i.e it should not open or close on click or swipe). But I am completely struct how to achieve the things by making the toggle event standstill. I know that my question is quite different but my requirement is to control dynamically on a runtime to control the property of drawertoggle.
I am also posting piece of code I am using for drawertoggle in my project for your reference
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
findViews();
createFragments ();
materialColorNames = getResources ().getStringArray(R.array.color_names);
drawerAdpater = new DrawerAdapter(this,materialColorNames);
mDrawerList.setAdapter(drawerAdpater);
if (toolbar != null) {
changeTitleText ("Droid");
toolbar.setTitleTextColor (getResources ().getColor (R.color.droid_white));
setSupportActionBar (toolbar);
}
initDrawer();
updateFragment (0);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onResume () {
super.onResume ();
}
private void changeTitleText (CharSequence title) {
toolbar.setTitle(title);
}
private void createFragments () {
myProjectsFragment = new MyProjectsFragment();
}
private void findViews() {
mDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
private void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// mDrawerLayout.closeDrawer(mDrawerList);
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
private void updateFragment(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
ft.replace(R.id.content_frame, myProjectsFragment);
break;
}
Kindly please help me with this solution. I am seraching this to be done for the long time. Thanks in advance. Please let me know through comments if my question is not clear.
In your onCreate() after drawer layout and list are found call:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerList);
drawerToggle.setDrawerIndicatorEnabled(false);
From the Javadoc:
public void setDrawerLockMode (int lockMode, View drawerView)
Enable or disable interaction with the given drawer.
This allows the application to restrict the user's ability to open or close the given drawer. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.

Lock one of two fragments in DrawerLayout

I have two navigation fragments in the DrawerLayout. One navigation fragment in on the left and second is on the right.
In some cases I need to open left navigation fragment and lock it.
When I had only left navigation fragment I used drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
But when I have added second navigation fragment on the right, this method started to open and lock both panels.
How to open and lock only left navigation view in this case?
Change you code like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mRightDrawerList = (ListView) findViewById(R.id.right_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {
public void onDrawerOpened(View view){
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mLeftDrawerList);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mRightDrawerList);
}
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mLeftDrawerList);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mRightDrawerList);
}
invalidateOptionsMenu();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
This should work.

Android lollipop toolbar switch between open/close drawer and back button

I had standart navigation drawer, but now i'm trying to modify it, using toolbar.
Earlier my code looked like:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MAIN", "CREATE");
initViews();
setListeners();
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
});
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
NavigationDrawerFragment.java
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
((MainActivity) getActivity()).getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
if (mDrawerListView != null) {
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
Now i modified my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
initViews();
setListeners();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
shouldDisplayHomeUp();
getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
mBackCount++;
shouldDisplayHomeUp();
}
});
}
NavigationDrawerFragment.java
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
((MainActivity) getActivity()).getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
if (mDrawerListView != null) {
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
And shouldDisplayHomeUp function:
public void shouldDisplayHomeUp (){
boolean canBack = getFragmentManager().getBackStackEntryCount() > 0;
mNavigationDrawerFragment.getDrawerToggle().setDrawerIndicatorEnabled(!canBack);
}
But the back arrow is not shown
I tried call
getSupportActionBar().setDisplayHomeAsUpEnabled(canBack);
getSupportActionBar().setHomeButtonEnabled(canBack);
So the back arrow appers but clicking on it has no effect
From the docs:
To allow Up navigation with the app icon in the action bar, call
setDisplayHomeAsUpEnabled():
#Override public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true); }
This adds a left-facing caret alongside the app icon and enables it as an action
button such that when the user presses it, your activity receives a
call to onOptionsItemSelected(). The ID for the action is
android.R.id.home.
This means that you will have to implement your back routine on onOptionsItemSelected and check for R.id.home. To avoid calling the routine when you click on the hamburger menu check for canback too on onOptionsItemSelected.
http://developer.android.com/training/implementing-navigation/ancestral.html#up
EDIT
To archieve what you want you will have to implement your own navigation routine.
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(shouldBack()) {
//call onbackpressed or something
if(displayBackAgain)
return; //return after so you don't call syncState();
}else if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
mNavigationDrawerFragment.openDrawer();
mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();
}
});
}
To enable the backbutton icon just call getSupportActionBar().setDisplayHomeAsUpEnabled(true); to disable it just call mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();
I found a way to control the back button and the nav. It worked with me.
First , set up:
private void setupNav () {
this.toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(this.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.mActionBarDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, this.toolbar, 0, 0);
this.mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//catch back button here.
}
});
this.mDrawerLayout.setDrawerListener(this.mActionBarDrawerToggle);
this.mActionBarDrawerToggle.syncState();
}
Important thing, this is the way I hide the hamburger and show the back button. You have to put this code in the place you want to show back button. I also lock the Nav when showing back button.
if (!isShowBackButton) {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//enable back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

How do I change my button image with navigation drawer opening and closing

I have a navigation button for my navigation fragment which turns active and opens a navigation drawer menu upon click:
Now when I click it, it turns active as follows:
However, I want to associate it with a navigation drawer such a way that, even if I do not click the button and slide open the navigation drawer, the button turns active when the navigation drawer menu is open and when closed by sliding back in from right to left, the button turns red/inactive. The code which I am trying to work with is as follows:
private boolean mIsNavigationOpen = false;
private DrawerLayout drawerLayout;
private NavigationPanelFragment dlDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
public boolean isNavigationOpen() {
return mIsNavigationOpen;
}
//----------Code for Navigation open logo button active/inactive instances
#SuppressWarnings("deprecation")
public void setNavigationOpen(final boolean isNavigationOpen) {
this.mIsNavigationOpen = isNavigationOpen;
final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
if(isNavigationOpen) {
mainButton.setBackgroundResource(R.drawable.bg_helios_active);
} else {
mainButton.setBackgroundDrawable(null);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//----------Code for Navigation Drawer setup
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
// invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
private void setupOnClickListenerForMainButton() {
final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
toggleNavigationPanel();
}
});
}
#Override
public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggleNavigationPanel();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
public void onNewsClicked(final View view) {
if(mIsNavigationOpen) {
toggleNavigationPanel();
}
if (isFragmentVisible(NewsFragment.TAG_NEWS_FRAGMENT)) {
return;
}
FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
mActiveFragment = NewsFragment.newInstance(getSupportFragmentManager());
updateActionBarTitle();
//For swipe action close drawer on button click
drawerLayout.closeDrawer(R.id.drawer);
}
public void onListsClicked(final View view) {
if(mIsNavigationOpen) {
toggleNavigationPanel();
}
if (isFragmentVisible(ListsContainerFragment.TAG_LIST_CONTAINER_FRAGMENT)) {
return;
}
FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
mActiveFragment = ListsContainerFragment.newInstance(getSupportFragmentManager());
updateActionBarTitle();
//For swipe action close drawer on button click
drawerLayout.closeDrawer(R.id.drawer);
}
private void toggleNavigationPanel() {
//final FragmentStackManager manager = FragmentStackManager.getInstance();
if (mIsNavigationOpen) {
//NavigationPanelFragment.removeInstance(getSupportFragmentManager());
updateActionBarTitle();
drawerLayout.closeDrawer(R.id.drawer);
} else {
drawerLayout.openDrawer(R.id.drawer);
final TextView title = (TextView) findViewById(R.id.main_title);
title.setText(getString(R.string.title_applications));
//NavigationPanelFragment.newInstance(getSupportFragmentManager(), manager.getTopTitle());
}
setNavigationOpen(!mIsNavigationOpen);
}
You might want to concentrate on the main_button and togglenavigationpanel. I added the condition if(drawerlayout.isdraweropen(R.id.drawer)){closedrawerlayout...}
but it didn't do the trick. I was wondering if anyone has any idea regarding the same?
Thanks!
When you've set up the drawer a listener has been added which has callbacks for when the drawer is in either final state, so what you need to do is reset the drawable inside these callbacks. I've shown what I think your final code should look like, minus some cleaning up you need to do, hope it helps
private DrawerLayout drawerLayout;
private NavigationPanelFragment dlDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ImageButton mDrawerButton; //Store it! - findViewById is *Expensive*
private TextView mTitle;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTitle = (TextView) findViewById(R.id.main_title);
mDrawerButton = (ImageButton) findViewById(R.id.button_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
mDrawerButton.setImageResource(R.drawable.bg_helios_inactive);
}
public void onDrawerOpened(View drawerView) {
mDrawerButton.setImageResource(R.drawable.bg_helios_active);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
setupOnClickListenerForMainButton(;
}
#Override //Note: I'd imagine this should be in onResume ...
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
private void setupOnClickListenerForMainButton() {
mDrawerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
toggleNavigationPanel();
}
});
}
...
private void toggleNavigationPanel() {
//if ( drawerLayout.isOpen() ) { //I dont know what methods these objects have
if ( dlDrawer.isOpen() ) { //but one of these must be sensibly storing its own state
updateActionBarTitle();
drawerLayout.closeDrawer(R.id.drawer);
} else {
drawerLayout.openDrawer(R.id.drawer);
mTitle.setText(getString(R.string.title_applications));
}
}

Categories

Resources