Navigation drawer is not closing even after closeDrawers() - android

I am creating a app with navigation drawer which is used to navigate through activities.
Here's my code or drawer
private void initInstances() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerToggle = new ActionBarDrawerToggle(busr.this, drawerLayout, R.string.hello_world, R.string.hello_world);
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.closeDrawers();
navigation = (NavigationView) findViewById(R.id.navigation_view);
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.navigation_item_1:
startActivity(new Intent(busr.this, MainActivity.class));
break;
case R.id.navigation_item_2:
startActivity(new Intent(busr.this, aff.class));
break;
case R.id.navigation_item_3:
startActivity(new Intent(busr.this, webs.class));
break;
case R.id.navigation_item_4:
startActivity(new Intent(busr.this, admnActivity.class));
break;
case R.id.navigation_item_5:
startActivity(new Intent(busr.this, busr.class));
break;
case R.id.navigation_item_6:
startActivity(new Intent(busr.this, trng.class));
break;
case R.id.navigation_item_7:
startActivity(new Intent(busr.this, prospct.class));
break;
case R.id.navigation_item_8:
startActivity(new Intent(busr.this, erp.class));
break;
case R.id.navigation_item_9:
startActivity(new Intent(busr.this, result.class));
break;
}
return false;
}
});

try this: For closing the drawerlayout
case R.id.navigation_item_1:
if (drawerLayout.isDrawerOpen(Gravity.START))
drawerLayout.closeDrawer(Gravity.START);
startActivity(new Intent(busr.this, MainActivity.class));
break;
or
case R.id.navigation_item_1:
drawerLayout.closeDrawers();
startActivity(new Intent(busr.this, MainActivity.class));
break;

Related

Navigation drawer just following one instruction

I am using a navigation drawer and want it to do different activities for different cases. I am using the switch method to implement the activities. I have implemented 3 switch cases but however, it keeps doing the case 3 instead of case 2. Here is my code that i am using:
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Discover");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
}
return true;
})
.build();
You forgot to add break; at the end of each switch case.
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
break;
}
Hope this will help!!

how to start a new activity by bottom navigation

I want to start new activity by bottom navigation bar but it not working.
I have checked many tutorials also, but do not know where problem the problem is.
Here is my code.
public class display extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
Intent i = new Intent(display.this, Addab.class);
startActivity(i);
break;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
break;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
break;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull final MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
startActivity(new Intent(this, Abc.class));
Break;
case R.id.navigation_camera:
startActivity(new Intent(this, Cab.class));
Break;
}
finish();
return true;
}
Also Refer here-->
https://blog.fossasia.org/using-activities-with-bottom-navigation-view-in-phimpme-android/
instead of break; use return true;

How to go the respective activity after clicking option in slide menu

ppl am trying to do menu option...and i got it,but after clicking the option in the slide menu...the control should go the respective page..i tried ..but its not working..please help me
switch(position){
case '0': {
startActivity(new Intent(home.this, home.class));
finish();
}
case '1':
{
startActivity(new Intent(home.this, info_values.class));
finish();
}
}
You should try like this
switch(position){
case 0:
startActivity(new Intent(home.this, home.class));
finish();
break;
case 1:
startActivity(new Intent(home.this, info_values.class));
finish();
break;
default:
break;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 'R.id.a':
startActivity(new Intent(home.this, home.class));
case 'R.id.b':
startActivity(new Intent(home.this, info_values.class));
}
return true;
}

NavigationDrawerItem'sClickevent

I created NavigationView and set 3 items.
As described in below picture :
Now what I want is, when I click paticular item, it should start an activity.
How to implement that?
Thanks.!!
What I can understand from your question is that you have used NavigationView for navigation drawer and you must have used menu file for your three menu option as displayed in the image you have shared. You need to handle those three cases in switch-case as given below. Change the menu item id according to your code and you are good to go.
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
//To close the menu drawer once it is clicked
drawerLayout.closeDrawers();
switch (menuItem.getItemId()){
case R.id.menu_item_appointments: //R.id.menu_item_appointment - menu item id which you mentioned in the menu file
// Code to start activity
return true;
case R.id.menu_item_upload_status:
//code to start activity
return true;
default:
Toast.makeText(getApplicationContext(),"Oops! Something went wrong.",Toast.LENGTH_SHORT).show();
return true;
}
}
});
I assumes that you are using listview in your Navigation drawer, which is a better way, now Check below code:
//to listen click events:
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
Here, position is the item no. of the navigation drawer item clicked. You can use a switch case to identify the item clicked and then call to startActivity using
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
Your selectItem should be like this:
private void selectItem(int position) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(currentActivity, NewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
currentActivity.startActivity(intent);
break;
case 1: // and so on.
Try this
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_item_1:
menuItem.setChecked(true);
Intent intent1 = new Intent(this, Activity1.class)
startActivity(intent1);
return true;
case R.id.navigation_item_2:
menuItem.setChecked(true);
Intent intent2 = new Intent(this, Activity2.class)
startActivity(intent2);
return true;
case R.id.navigation_item_3:
menuItem.setChecked(true);
Intent intent3 = new Intent(this, Activity3.class)
startActivity(intent3);
return true;
case R.id.nav_sub_menu_item01:
menuItem.setChecked(true);
Intent intent = new Intent(this, Activity1.class)
startActivity(intent);
return true;
}
}
Create an OnNavigationItemSelected like the below example .
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_project) {
Intent i = new Intent(HomeActivity.this, Project.class);
startActivity(i);
} else if (id == R.id.nav_gallery) {
Intent i = new Intent(HomeActivity.this, Gallery.class);
startActivity(i);
} else if (id == R.id.nav_contact) {
Intent i = new Intent(HomeActivity.this, Contact.class);
startActivity(i);
} else if (id == R.id.nav_login) {
Intent i = new Intent(HomeActivity.this, Login.class);
startActivity(i);
} else if (id == R.id.nav_favourites) {
Intent i = new Intent(HomeActivity.this, Favourite.class);
startActivity(i);
} else if (id == R.id.nav_share) {
}
}
add these two items in your strings.xml
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

Stop rebuilding fragment again and again

In my application I am giving navigation drawer by these lines of code
switch (menuItem.getItemId()) {
case R.id.home:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.list_view_container, fragment).commit();
return true;
case R.id.current_event:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.college_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.national_events:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.workshops:
menuItem.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.trainings:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, "Launching " + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.companies:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.talents:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.games:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.submission:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.about:
menuItem.setChecked(true);
Toast.makeText(MainActivity.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
Each time new selection goes on fragments get rebuilt again and again I just want to just pause each fragment at its last state and wake up on their selection without getting rebuilt.
you should use something like this:
case 1:
if(getSupportFragmentManager().findFragmentByTag("ranking")==null) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_holder,
new RankingFragment(), "ranking").
addToBackStack("ranking").
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
} else {
getSupportFragmentManager().popBackStack("ranking", 0);
}
break;

Categories

Resources