I have an activity with a navigation drawer that works as it should. The only problem is that the hamburger menu doesn't animate after the first fragment replacement.
public class Main extends AppCompatActivity {
// lots of attributes
#Override
protected void onCreate(Bundle in) {
super.onCreate(in);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
setupDrawer();
}
public void onEventMainThread(LoadedLEsEvent event) {
setupDrawer();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
void setupDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, navDrawer, R.string.drawer_open, R.string.drawer_closed) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
navDrawer.setDrawerListener(drawerToggle);
menuListAdapter = new MenuListAdapter(this, R.layout.drawer_item, someList);
menuListView.setAdapter(menuListAdapter);
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Replaces the frame layout with a fragment
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
So basically it works fine until I replace the FrameLayout with a fragment. From here, the drawer still works but the Hamburger menu doesn't animate at all.
So the problem was that I initialized drawerToggle each time I called setupDrawer(). Moving drawerToggle = new ActionBarDrawerToggle(...) {...}; to onCreate() did the trick.
Related
I am developing a quiz app which consists of drawer layout in all the activities.The contents in the drawer is same and i want it in all the activities.By searching google i found a solution that declaring drawer in one activity and inherit in all the activities,it works well too.I am attaching the code below.
Now the problem is that i need Tab in one of by activity so i don't know how to inherit 2 activity in here[which is not possible].
Can any one help me out with this problem?
Simply saying,
I have a option that i can extend only i class what to do if i want two classes?
DrawActivity.java
public void onCreateDrawer() {
// R.id.drawer_layout should be in every activity with exactly the same id.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(DrawActivity.this,drawerLayout,R.drawable.ic_drawer2,R.string.app_name,R.string.app_name)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(R.string.app_name);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//getActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged","").toString().equals("logged")) {
layers = getResources().getStringArray(R.array.layers_array1);
flag=0;
}
else {
layers = getResources().getStringArray(R.array.layers_array);
flag=1;
}
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,layers));
drawerList.setSelector(R.color.darkgreen);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
selectItemFromDrawer(pos);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
I am creating simple app having DrawerLayout navigation. In which one menu item is My Profile. In My profile Screen there is button which open Change Password screen in same Fragment. if i open DrawerLayout 's menu and close it without clicking, then My Profile Screen loads again.
Following is my code
public class HomeActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ArrayList<SideMenuEntity> listSideMenuItems;
private SideMenuAdapter adapterSideMenu;
Menu menu;
Fragment fragment;
AsyncTaskHelper loadFragmentTask;
int position, old_position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
listSideMenuItems = new ArrayList<>();
for(String str : Constant.SIDE_MENU_ITEMS)
{
listSideMenuItems.add(new SideMenuEntity(str));
}
// setting the nav drawer list adapter
adapterSideMenu = new SideMenuAdapter(getApplicationContext(),listSideMenuItems);
mDrawerList.setAdapter(adapterSideMenu);
mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
displayView(position);
}
});
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.mipmap.ic_launcher,R.string.app_name, R.string.app_name)
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
if (fragment != null) {
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
old_position = -1;
displayView(-1);
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_home_setting, menu);
_setActionBarHomeVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId())
{
case R.id.title_bar_home:
displayView(0);
return true;
case R.id.title_bar_setting:
_setFragmentContainer(new SettingFragment(), "Setting");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
*
*/
protected void displayView(int p)
{
position = p >= 0 ? p : 0;
fragment = null;
switch (position)
{
case Constant.SIDE_MENU_ITEM_DAHSHBOARD:
fragment = new DashboardFragment();
break;
case Constant.SIDE_MENU_ITEM_MY_PROFILE:
fragment = new MyProfileFragment();
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**-------------- private functions ---------------------*/
public void _setFragmentContainer(Fragment fragment, String title)
{
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
setTitle(title);
}
}
Try adding the super constructor:
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
if (fragment != null) {
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
}
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
i solve the problem my self own. actually whenever DrawerLayout closed, onDrawerClosed event fired, then i just null the fragment variable after _setFragmentContainer() function
if (fragment != null)
{
_setFragmentContainer(fragment, listSideMenuItems.get(position).title);
fragment = null;
}
Now DrawerLayout Menu closed, due to fragment null , it don't load fragment again.
I'm trying to implement an ActionBarDrawerToggle into my app but I can't make it. I've achieved to show the toggle in my toolbar, but the icon is the same always.
This is toggle's icon when drawer is closed:
http://i.stack.imgur.com/HTcom.png
And this is when drawer is opened:
http://i.stack.imgur.com/dX0Z2.png
As you can see, it doesn't change from ic_drawer to back arrow. ic_drawer is never shown.
So, here's my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initializeDrawer();
populateDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void initializeDrawer() {
tagTitles = getResources().getStringArray(R.array.item_names);
icons = getResources().getStringArray(R.array.item_icons);
dwLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dwList = (ListView) findViewById(R.id.drawer_list);
dwList.setOnItemClickListener(new DrawerItemClickListener());
dwToggle = new ActionBarDrawerToggle(this, dwLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle("pepe");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("pop");
invalidateOptionsMenu();
}
};
dwLayout.setDrawerListener(dwToggle);
}
private void populateDrawer() {
ArrayList<DrawerItem> dwItems = new ArrayList<>();
for (int i = 0; i < tagTitles.length; i++) {
//ignore this; population is not properly working
dwItems.add(new DrawerItem(tagTitles[i], Resources.getSystem().getIdentifier("abc_ic_menu_copy_mtrl_am_alpha.png", "drawable", getBaseContext().getPackageResourcePath())));
}
dwList.setAdapter(new NavigationDrawerAdapter(this, dwItems));
}
#Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
dwToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (dwToggle.onOptionsItemSelected(item)) {
return true;
}
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
dwToggle.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
Thanks!
implement following method with DrawerLayout instace, in your case instance is dwLayout. implement following after this line
dwLayout.setDrawerListener(dwToggle);
add this
drawerLayout.post(new Runnable() {
#Override
public void run() {
// To display hamburger icon in toolbar
drawerToggle.syncState();
}
});
or you can do this as well
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
My question is very clear that i want to change the Up icon When NavigationDrawer open and closed. So please tell me how to Change the upp icon in anction bar. I have tried so much but i m not able to do. ii have used custom layout to set the title. Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.center_action_bar_text);
actionBar.setTitle("dvds");
View view = actionBar.getCustomView();
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText("zfdgfdg");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
---------------------------------
drawerListView.setAdapter(drawerListAdapter);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(HomeActivity.this,
drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, 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);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
Add this method to your code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
i have other one answer
just add this method:-
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider);
//menu.findItem(R.id.sliding).setVisible(!drawerOpen);
if(drawerOpen)
{
menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
}
else {
menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon);
}
//menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher);
//menu.findItem(R.id.action_settings).setVisible(!drawerOpen1);
return super.onPrepareOptionsMenu(menu);
}
for that method call you need to use this tow method
invalidateOptionsMenu();
// calling onPrepareOptionsMenu() to hide action bar icons
when menu is open
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
//mDrawerLayout.openDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
when menu is close
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
//menu.findItem(R.id.sliding).;
//mDrawerLayout.closeDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
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));
}
}