I am using DrawerLayout with custom side menu (layout) the tittle of the Action Bar needs to be changed when drawer is open or when drawer is closed. i have tried to achieve that on onOptionsItemSelected() by toggling the tittle on drawer icon press. but when drawer is swiped open or closed this approach is not working
public class MainActivity extends AppCompatActivity {
private boolean isOpen ;
private DrawerLayout mDrawerlayout ;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle(R.string.drawerclosed);
// ActionBar bar = getActionBar();
// bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
mDrawerlayout = (DrawerLayout) findViewById(R.id.Drawer) ;
mToggle = new ActionBarDrawerToggle(this,mDrawerlayout,R.string.draweropen,R.string.drawerclosed) ;
mDrawerlayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{
if(!isOpen)
{
isOpen = true ;
getSupportActionBar().setTitle(R.string.draweropen);
}
else if(isOpen)
{
isOpen = false ;
getSupportActionBar().setTitle(R.string.drawerclosed);
}
return true ;
}
return super.onOptionsItemSelected(item);
}