I have a navigaiton drawer. When i click on a certain item from the nav drawer, the nav drawer is closed and is redirected to main page. I want navigation drawer to refresh after clicking an item in it.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (id == R.id.refresh_drawer) {
----CODE----
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
What can i do to make nav drawer refresh and not redirect to main page? somebody please help me
It closes because you used:
drawer.closeDrawer(GravityCompat.START);
In onNavigationItemSelected() method so, removing this will help.
Also:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Define this inside onCreate() method and make it private by adding it in above of onCreate() like this to have access whole the Activity:
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
}
This is the recommended way to define such widgets inside an Activity when Activity creates.
Related
I am using MaterialDrawer from:
https://github.com/mikepenz/MaterialDrawer
and when the user presses the back button, the app goes one activity backwards, instead of just closing the drawer.
is it a bug or there is a way to fix it?
This is expected behavior. If you want to close Drawer on Back press, you have to override the onBackPressed() function.
First get a reference to your DrawerLayout :
DrawerLayout myDrawer = (DrawerLayout) findViewById(R.id.my_drawer);
Then override the onBackPressed() function to close drawer when it is open instead of closing activity:
#Override
public void onBackPressed() {
if (myDrawer.isDrawerOpen(GravityCompat.START)) {
myDrawer.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
Hope it helps.
just close your drawer if it is open on pressing back button
#override
public void onBackPressed(){
//DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
DrawerLayout drawerLayout = result.getDrawerLayout();
if(drawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
drawerLayout.closeDrawer(GravityCompat.START);
//result.closeDrawer();
}
}
I'm working with Navigation Drawer and I want to click the button on activity and Navigation Drawer should show, but I don't know how to do it. Normally, when we swipe from left edge to the right the navigation will be shown. as such, I want to be able to open the navigation drawer with the click of a button.
Use the following methods to open and close your navigation drawer:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//To Open:
drawerLayout.openDrawer(Gravity.START);
//To Close:
drawerLayout.closeDrawer(Gravity.END);
Source1 for more information:
Source2
Button hamMenu = findViewById(R.id.ham_menu);
hamMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
// If the navigation drawer is not open then open it, if its already open then close it.
if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
else navDrawer.closeDrawer(Gravity.END);
}
});
You can also use a method and Use the exact same steps given by #Zeeshan.
public void hamMenu(View view){
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (!drawer.isDrawerOpen(GravityCompat.START)) {
drawer.openDrawer(GravityCompat.START);
} else {
drawer.closeDrawer(GravityCompat.END);
}
}
Use this method in the button which you need android:Onclick="hamMenu" in .xml.
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);
What i want here when i move to another Fragment by clicking in NavigationDrawer menu button then button should be disabled.
Because addToBackStack(); method add multiple times in their stack when click again and again. So wanted to disable it when i move to another fragment.
To disable toggle button in navigation drawer use
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
to enable use LOCK_MODE_LOCKED_OPEN replacing LOCK_MODE_LOCKED_CLOSED
To disable drawer item click
Refer Hide Some Navigation Drawer Menu Item - Android there you can hide it
If you do not wish to hide in onNavigationItemSelected where you check (id == R.id.whatevertheitemid) also use a Boolean to allow access as you wish
eg
if (id == R.id.whatevertheitemid && isAccessGiven) { // do your task
}
Its helps me to solve this issue:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_item1){ //use can write your menu item here
return false;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Use this to disable menu item:
NavigationView navigationView;
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Now in menu item click:
Menu menuView= navigationView.getMenu();
menuView.getItem(ID).setEnabled(false);
in kotlin:
navigation.menu.findItem(R.id.your_target_item_id).setEnabled(false)
this is work for me and disabled item in drawerToggle menu
in Java:
NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
navigation.getMenu().getMenu().findItem(R.id.your_target_item_id).setEnabled(false);
I have created a program, I have the base nav bar extended like so:
public class navBar extends AppCompatActivity {}
The navbar code itself is too long to include, but it sets a toolbar, and sets a navbar and works great when I use it in most parts of my app.
However, when I try to add another navbar in the extended class it does something odd, when I press the points of interest button to open it and it runs this code:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.POI)
mDrawerLayout.openDrawer(mDrawerList);
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
It opens the drawer as requested, but leaves no dimming on the screen. Like so:
When I close the screen with the back button/points of interest button for example it lags a bit, dragging the white slowly but closes.
Even worse, when I had opened the drawer with a
However, when I use my finger to drag from right to left to open the drawer, it opens and functions perfectly.
Because my map class is rather long as well, I've tried to include only the important code:
public class Map2 extends navBar {
private Toolbar toolbar;
ActionBarDrawerToggle mDrawerToggle;
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);
toolbar = (Toolbar) findViewById(R.id.nav_base_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
mDrawerList.clearChoices();
super.onDrawerClosed(view);
//getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.right_drawer);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.right_drawer);
POIAdapter_without_image adapter2 = new POIAdapter_without_image(this, R.layout.points_of_interest, drawerItem2);
mDrawerList.setAdapter(adapter2);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggleChild.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_skill_view, menu);
return true;
}
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
}
else
super.onBackPressed();
}
Edit:
Whenever I select a point on the points of interest list, the dragging white line can be seen here moments after the actual drawer has closed:
The black flash mentioned appears to be android both setting the dim and unsetting the dim as soon as I close the drawer.
This has thoroughly confused me for a few nights now. Thank you all!
you setDrawerShadow to cast shadow to drawer
drawer_layout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
Update :
drawer_shadow is .png file you put in drawable folder.
START : when drawer opens from left side.
END : when drawer opens from right side
In the end, the answer was to call:
final FrameLayout mapView = (FrameLayout) getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);
and when I wanted to find anything use
mDrawerLayout2 = (DrawerLayout) mapView.findViewById(R.id.drawer_layout);
for example.
it was then necessary to call
mDrawerLayout2.closeDrawer(GravityCompat.END);
rather than
mDrawerLayout2.closeDrawer(mDrawerList);
To obtain the needed functionality