I'm developing an Android application, I use navigation drawer as layout, i would know if is It possible that when the application starts the side menu remains open.
Actually At startup the menĂ¹ remains closed.
This line:
drawer.openDrawer(Gravity.LEFT);
will open your drawer, so you just need to call it on start up of your activity, for example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer.openDrawer(Gravity.LEFT); // you can change the gravity to right if your drawer is rtl
}
any problem, just ask
Try using
drawer.openDrawer(Gravity.LEFT);
where
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
Related
I have a pretty standard DrawerLayout in my app. In this particular app I need the drawer to only open with an icon click, not swiping, so the drawer lock mode is set to LOCKED_CLOSED unless the user presses the proper icon.
To close the drawer I want the user to be able to use the drawer icon, the home icon on the action bar OR the native Back button. The first two work just fine, but the back button really doesn't. Here's the code:
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawer.closeDrawer(GravityCompat.END);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}else{
super.onBackPressed();
}
}
What am I doing wrong? Should I be calling the set lock mode methods on a drawer layout object instead of the drawer?
I'm working on an Android App, I have a navigation drawer over there. Since the navigation drawer toolbar can't be transparent, and the ending three dots button icon can't be changed, I opted for hiding that toolbar, and show my custom layout. It will give me all the functionality what ever is needed.
But the problem I'm facing right now is, once the activity starts, if I click the custom menu button it doesn't open. Once I drag it and open, after that whenever I click the menu button it opens the navigation drawer.
What might i be missing? This is what I'm doing, while debugging its even coming to the else part, but doesn't open.
In BaseActivity:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ivLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.LEFT)) {
drawer.closeDrawer(Gravity.LEFT);
} else {
drawer.openDrawer(Gravity.LEFT);
}
}
});
In any of the child activity:
toolbar.setVisibility(View.GONE);
navigationView.setVisibility(View.GONE);
Please help..
The root cause of your problem is the fact that you're setting the drawer View's visibility to GONE. The direct cause of the odd behavior you describe, though, is due to how DrawerLayout and one of its helper classes update the child Views when the drawer state changes.
The OnClickListener you set to open and close the drawer was working as it should. It just didn't appear to be, since the drawer View was GONE. When you manually opened the drawer by dragging, however, the ViewDragHelper that DrawerLayout uses was firing a callback method that explicitly sets the drawer to VISIBLE. This callback is not fired when the drawer is opened programmatically - that is, with the openDrawer() method - which explains why the drawer didn't show just by clicking your custom toggle button. After you had dragged the drawer open once, the drawer View was visible, and the toggle would then work as expected.
The drawer View is in its closed state by default, so you don't need to hide it, and you can just remove the navigationView.setVisibility(View.GONE); line.
I have an app with navigation drawer. Everything works good, however my navigation drawer opens automatically as my app starts.
I do not want to see my navigation drawer unless I swipe of click to menu button.
Thanks in advance!
When you launch the navigation drawer activity. Make sure it has been already opened.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
}
Call YourDrawerLayout.closeDrawers(); in your main activity, on OnCreate method, to close navigation drawer.
For closing..
private void closeDrawer() {
if (mDrawer.isDrawerOpen(GravityCompat.START)) {
mDrawer.closeDrawer(GravityCompat.START);
}
}
I've just begun work on a small little app and would like to change the Toolbar title to the name of the fragment the user has selected from the nav drawer, however upon setting the Toolbar as the action bar to be able to use :
getSupportActionBar.setTitle("[insert category here]")
The toolbar's hamburger button stops opening the nav drawer when clicked. (You can still open it via dragging in from left).
Does anyone know how to fix this and/or does anyone know of another method to change the toolbar's title? (I found this method via googling).
Try this in your Activity:
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
And this is for your Fragment(you can use it on OnCreate or onResume method):
#Override
public void onResume(){
super.onResume();
// Set the title
((MainFragmentActivity) getActivity()).setActionBarTitle("Your title");
}
Also, have a look at:
Change ActionBar title using Fragments
Setting Custom ActionBar Title from Fragment
this worked well with me hope it helps anyone facing the same problem (this fix the problem when you press on the hamburger icon to open the drawerLayout)
setSupportActionBar(binding.appBarMain.toolbar)
//to open the navBar by pressing on the hamburger Icon
ActionBarDrawerToggle(this, binding.drawerLayout,binding.appBarMain.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
Can we launch navigation drawer without using ActionBarDrawerToggle? In my implementation, I do not want to use action bar.
So, I don't want ActionBarDrawerToggle to open and close the drawer. I want to open the navigation drawer when hardware menu button is clicked.
I am targeting Android 2.2 and using support library v4.
As per my understanding, ActionBarDrawerToggle class will have implementation internally to handle the opening and closing of navigation drawer. Am I correct?
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ImageView ivNavigationIcon = //some image or button or menu what ever you want
ivNavigationIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
});
I want to open the navigation drawer when hardware menu button is clicked
Not every Android device has a MENU button, so you better have some other solution beyond just that.
Also, using MENU to open a navigation drawer, instead of a menu -- may be viewed as odd behavior by some of your users.
I am targeting Android 2.2 and using support library v4.
If you are using DrawerLayout, it has openDrawer() and closeDrawer() methods that you can use that will open the drawer and close the drawer, respectively.