I am trying to switch to the new NavigationView for my Drawer.
When i did (and its working fine), I lost the animation of the Menu icon to the Back icon when the Drawer is sliding.
How can i have that on the NavigationView?
Here is my code:
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white);
setSupportActionBar(toolbar);
mDrawer = (NavigationView) findViewById(R.id.navigation_drawer);
if (mDrawer != null) {
mDrawer.setNavigationItemSelectedListener(this);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Thanks in advance!
After doing a lot research, i solved it by simply adding the following:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
Hope it helps.
Related
When I go back after changing it to a back button it disappears with the following code:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerListener(mToggle);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
I think that code should make the navigation drawer button to open menu to appear again, but it doesn't.
Any idea of what could be wrong in that code, so the navigation drawer button appears again?
Try like this:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//.....
Also, you could use androidx.navigation.ui.NavigationUI package to setup your drawer menu.
Here are good articles about NavigationUI.
I am developing an android application with a navigation drawer. Everything works fine. except that when I start my application the drawer appears automatically. Here what I tried to fix this issue:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
mDrawerLayout);
// as a solution I added this if-statement
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawers();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
But it is not working! . How to hide it once the applilication starts ?
mDrawerLayout.closeDrawer(GravityCompat.START);
Hi u can use following code:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
Details: After changing the hamburger icon into a custom icon it does not respond on clicking (drawer does not open)
Here is the code snippet for oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contentViewId());
toolbar = (Toolbar) findViewById(toolbarId());
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (toolbarTitle() != null || !toolbarTitle().contentEquals(""))
getSupportActionBar().setTitle(toolbarTitle());
}
drawerLayout = (DrawerLayout) findViewById(drawerLayoutId());
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(drawerToggle);
navigation = (NavigationView) findViewById(navigationViewId());
navigation.setNavigationItemSelectedListener(this);
navigation.getMenu().findItem(selectedMenuItem()).setChecked(true);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_account_balance_black_24dp);
drawerToggle.syncState();
}
More details:
the hamburger icon do change and also it respond when opening the drawer through slide but when i click the custom icon it does not..
Remove this line:
drawerToggle.setDrawerIndicatorEnabled(false);
I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.
I have tried many times but I am unable to change the icon with my own icon .
Can anyone help me ?
new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withToolbar(toolbar)
.addDrawerItems(
//pass your items here
)
.build();
CODE TO SHOW THE HUMBURGER ICON:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
following is the code I found many times but i tried this also but it did not work
Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?
I haven't tried it with that library but, try the following:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.
For that you can do some thing like that
protected void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
...
}
Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.
protected void onCreate(Bundle savedInstanceState) {
...
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
...
}
You can update the icon dynamically whenever required as
toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
Hope this will help you.
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}
}
Again Do you have any quires get Consult me here.....,hope you got solution to your problem...
Try this by modify following:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
to
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
this disable library default icon then change the icon...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);
I'm having a problem with the Navigation Drawer Icon.
We replaced the default "back caret" to use a different icon and it works fine.
However, if the navigation drawer is already open and the user rotates their device, then the icon reverts back to the default caret and won't go back to the custom one until the navigation drawer is closed and the onCreate() method for the activity is called again (usually by rotating the device).
Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// set the toolbar as the action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
setSupportActionBar(toolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StartFragment())
.commit();
}
//Init the GameLog
GameLog.init(this);
}
/**
* Initializes the DrawerLayout for the particular activity
*/
public static void init(Activity activity) {
mActivity = activity;
mDrawerLayout = (DrawerLayout)activity.findViewById(R.id.drawer_layout);
mRecyclerView = (RecyclerView)activity.findViewById(R.id.left_drawer);
//set adapter
mRecyclerView.setAdapter(mAdapter);
//set layout manager
mRecyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
//add a divider between elements
mRecyclerView.addItemDecoration(
new HorizontalDividerItemDecoration.Builder(activity)
.color(Color.WHITE)
.build());
Toolbar toolbar = (Toolbar)((ActionBarActivity)activity).getSupportActionBar().getCustomView();
mDrawerToggle = new ActionBarDrawerToggle(activity, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (activity.getActionBar() != null) {
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setHomeButtonEnabled(true);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
GameLog.getToggle().syncState();
}
Hopefully this makes sense.
Thanks for any help.
Look into calling ActionBarDrawerToggle#syncState.
https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html#syncState()